miércoles, 23 mayo 2007

JTable, detecting selection changes // ListSelectionListener /*Selection Changed Event*/

« SimpleDateFormat to check user date input // parsing String to dates in java | Main | Dynamic icons for your JComponents // Create an icon JButton with dynamic icons. »

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 */
    }
});

Technorati Tags:

Posted by admin at 5:25 PM in Java

 

[Trackback URL for this entry]

Your comment:

(not displayed)
 

SCode

Please enter the code as seen in the image above to post your comment.

 
 

Live Comment Preview:

 
Google
 
« May »
SunMonTueWedThuFriSat
  12345
6789101112
13141516171819
20212223242526
2728293031