JTable, detecting selection changes // ListSelectionListener /*Selection Changed Event*/
When working with tables, it may be useful to detect selection changes. If for example, you need to sum the values of a specified column in a selection range, this method will be most essential.
If you explore the available methods to add listeners to a JTable
, you will notice there's no such thing as a selectionListener
. This is because the JTable
has its own selection model, where you can add the listener.
The following code illustrates the way to go to add a selection listener:
1JTable jTable = new JTable();
2jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
3 public void valueChanged(ListSelectionEvent e) {
4 /* TODO: Add whatever you need to do when the selection changes */
5 }
6});
Comments in "JTable, detecting selection changes // ListSelectionListener /*Selection Changed Event*/"