我无法在我的java项目中检测到点击次数= 2(双击)

问题描述 投票:0回答:1

我试图听取双击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。

java swing jtable mouselistener
1个回答
2
投票

mouseClicked取代mousePressed为我工作。我相信这是因为mouseClickmousePressedmouseReleased的组合。如果鼠标在两个事件之间移动甚至像素,则不会生成事件。

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");
                }
           }
   });
© www.soinside.com 2019 - 2024. All rights reserved.