我正在尝试侦听jtable的双击,但是鼠标侦听器仅收到点击计数= 1。
JTable replacedAssets = new JTable(...);
replacedAssets.addMouseListener ( new MouseAdapter ( ) {
@Override
public void mouseClicked ( MouseEvent e ) {
super.mouseClicked ( e );
if ( e.getClickCount () == 2 ) {
rowIndex = replacedAssets.getSelectedRow ();
columnIndex = replacedAssets.getSelectedColumn ();
if ( rowIndex == 0 && ( columnIndex == 1 || columnIndex == 2 ) ) {
initial = replacedAssets.getValueAt ( rowIndex , columnIndex );
JOptionPane.showMessageDialog ( parent , "Editing this Field may cause error in the data causing problems." , "Error Edit Not Permitted For This Field" , JOptionPane.ERROR_MESSAGE );
}
}
}
} );
这是我的鼠标侦听器代码,请帮助。
PS:它对JLabel正常工作。
最佳答案
用mouseClicked
替换mousePressed
对我有用。我相信这是因为mouseClick
是mousePressed
和mouseReleased
的组合。如果鼠标在两个事件之间移动甚至一个像素,也不会生成事件。
replacedAssets.addMouseListener (new MouseAdapter(){
@Override
public void mousePressed(MouseEvent mouseEvent) {
super.mousePressed(mouseEvent);
JTable table = (JTable) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2 ) {
System.out.println("Double click detected");
}
}
});
关于java - 我在Java项目中无法检测到点击计数= 2(双击),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52345680/