JTable cell listener?

tadpole picture tadpole · Sep 11, 2012 · Viewed 19.4k times · Source

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 ?

Answer

parsifal picture parsifal · Sep 11, 2012

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.