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 ?
2 Answers
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.

- 266
- 2
- 3
-
I was already doing option #1. The problem is the listener is called only when the row changes. So for example, if you go from 0,0 to 0,1 there listener does not call. So this option does not do any good. For option 2, I could not find the listener for the column selection listener. There is one for value change, but I need a selection listener not value changed. – tadpole Sep 11 '12 at 20:41
-
1@tadpole - `JTable.getColumnModel().getSelectionModel()` – parsifal Sep 11 '12 at 20:45
-
@tadpole registering a TableColumnModelListener and implement its columnSelectionChanged method is the direct way, though going one step deeper and interact directly with the column selection model is shorter :-) BTW, you might consider to edit your answer and remove the first part – kleopatra Sep 12 '12 at 07:18
-
+1 -- the "alternative" finally worked for me. (I couldn't use the first option because I could only have a SINGLE cell selected.) – Xynariz Dec 07 '13 at 06:51
-
2I found that I needed to add the same listener for both the `ColumnModel`'s `SelectionModel` and the `JTables`'s `SelectionModel` to get it to recognize both row and column changes. – clum May 27 '15 at 19:39
One way to receive notification on column selection changes - as already answered by @parsifal(in the comments - is to grab the TableColumnModel's internal selectionModel and register a listener:
table.getColumnModel().getSelectionModel().addListSelectionListener(selectionListener);
Another way is to register a TableColumnModelListener with the columnModel:
table.getColumnModel().addColumnModelListener(columnModelListener);
The first is "shorter" in terms of code: just one method to implement vs. several - most empty except the columnSelectionChanged.
The second is more robust against dynamic changes: with the first there is no possibility to guard against changes of the selectionModel property of the columnModel ... because it is not a property. Or in other words: in the (concededly rare) case that application code swaps out the selectionModel the listener is listening to the Void. Installing a columnModelListener is immune against such a change, as the columnModel passes on the events from its selectionModel whichever it would be.

- 51,061
- 28
- 99
- 211