Search code examples
javaswingjtablemouseeventactionlistener

ActionListener on JLabel or JTable cell


I have a JTable with JLabel[][] as data. Now I want to detect a double click on either the JLabel or a table cell (but only in one of the columns). How can I add an Action/MouseListener on JLabel respectively table cell?


Solution

  • How about:

    table.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
          JTable target = (JTable)e.getSource();
          int row = target.getSelectedRow();
          int column = target.getSelectedColumn();
          // do some action if appropriate column
        }
      }
    });