Search code examples
javaswingjtablelistselectionlistener

JTable cell listener?


I am using a JTable. I need to get a notification whenever a cell selection change. I tried to use the ListSelectionListener but I only get notification when the row selection change. If I select a new column on the same row, I don't get notify. I need to know when the cell is selected, not when the cell is changed. Is there a listener that I can use to do this ?


Solution

  • The easiest way to do this is to call setCellSelectionEnabled(true), and pass a reference to your table to the listener. When the listener is invoked, call getSelectedRow() and getSelectedColumn() on the original table.

    The alternative is to set a row selection listener on the table, a column selection listener on the ColumnModel, and then figure out their intersection.