ActionListener on JLabel or JTable cell

stefita picture stefita · Sep 4, 2009 · Viewed 51.2k times · Source

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?

Answer

Vinay Sajip picture Vinay Sajip · Sep 4, 2009

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
    }
  }
});