[英]How to get the value of a JTable cell with a right mouse click
I have a JTable
in my Java Swing application and everything works fine in general. 我的Java Swing应用程序中有一个
JTable
,并且一切正常。 I have added a MouseListener
on my JTable
, so whenever I try right clicking on a row in the table, I can capture the event and run a method. 我在
JTable
上添加了MouseListener
,因此,每当尝试右键单击表中的一行时,我都可以捕获事件并运行一个方法。 However, what I'd like to do is to 但是,我想做的是
To do this, I must currently left click on the row, then also do a right click on it. 为此,我目前必须在该行上单击鼠标左键,然后右键单击它。 Is it possible to select the row/cell right away with only a single click of the right mouse button?
只需单击鼠标右键就可以立即选择行/单元格吗?
Here is my code so far: 到目前为止,这是我的代码:
public class MyMouseAdapterTableArticoli extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
JTable t = (JTable)me.getSource();
JMenuItem menuItem;
rowPopUp = t.rowAtPoint(me.getPoint());
if ((me.getClickCount() == 2) && (me.getButton() == MouseEvent.BUTTON1)) {
pulisciTableArtRappre();
if((listaMagazzino != null) && (listaMagazzino.size() > 0)) {
pulisciTableArtMagazzino();
}
popolaCampi(rowPopUp);
} else if (me.getButton() == MouseEvent.BUTTON3) {
JPopupMenu popup = new JPopupMenu();
menuItem = new JMenuItem("Mostra Prezzi di Acquisto");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mostraPrezziAcquisto(rowPopUp);
}//fine metodoVoid
});//fine actionlistener
popup.add(menuItem);
MouseListener popupListener = new PopupListener(popup);
table.addMouseListener(popupListener);
}//fine else e if
}
public void mousePressed(MouseEvent e) {
JTable source = (JTable)e.getSource();
int row = source.rowAtPoint(e.getPoint());
int column = source.columnAtPoint(e.getPoint());
if (!source.isRowSelected(row)) {
source.changeSelection(row, column, false, false);
}
}
table.addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
JTable source = (JTable)e.getSource();
int row = source.rowAtPoint( e.getPoint() );
int column = source.columnAtPoint( e.getPoint() );
if (! source.isRowSelected(row))
source.changeSelection(row, column, false, false);
}
});
Edit: 编辑:
Create a simple example when learning a new concept. 在学习新概念时创建一个简单的示例。 For example:
例如:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
JTable table = new JTable(15, 5);
add( new JScrollPane(table) );
table.addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
JTable source = (JTable)e.getSource();
int row = source.rowAtPoint( e.getPoint() );
int column = source.columnAtPoint( e.getPoint() );
if (! source.isRowSelected(row))
source.changeSelection(row, column, false, false);
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE(), BorderLayout.NORTH);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.