Search code examples
javaswingjtablemouseeventmouseclick-event

Detect click on JTable: sometimes not recognized


I've this code to detect click on JTable

table.addMouseListener(new java.awt.event.MouseAdapter()
{
    @Override
    public void mouseClicked(java.awt.event.MouseEvent e)
    {

        int row= table.rowAtPoint(e.getPoint());
        int col= table.columnAtPoint(e.getPoint());
        System.out.println(table.getSelectedRow());

        if (e.getClickCount() == 2)
        {

            System.out.println ("Doppio Click");

        }

    }

});

This code work great the problem is that if I click on a row and before mouse button up I move mouse up the click is not detected but the row is selected in my JTable. Can anyone know how to fix that problem? Thanks!


Solution

  • Use MouseListener.mouseReleased or MouseListener.mousePressed events instead (to detect double clicking that way is tricker but could be done).