我试图听取双击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 );
}
}
}
} );
这是我的鼠标监听器Code请帮忙。 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");
}
}
});