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:
JTable jTable = new JTable();
jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
/* TODO: Add whatever you need to do when the selection changes */
}
});
Comments in "JTable, detecting selection changes // ListSelectionListener /*Selection Changed Event*/"