Adding sorting to a JTable

sorter for JTable

To add buttons to a JTable’s headers so that when you click on the headers the JTable is sorted in those particular fields:

JTable table = new JTable();
table.setAutoCreateRowSorter(true);

This is also helpful as the underlying TableModel is not altered. But to use selected cells one needs to use the following commands:

//for column indices
table.convertColumnIndexToModel(viewColumnIndex);
table.convertColumnIndexToView(modelColumnIndex);

//for row indices
table.convertRowIndexToModel(viewRowIndex);
table.convertRowIndexToView(modelRowIndex);

Note that one can also extend off “DefaultTableModel” and add other helper functions to the table model such as row counts, column counts etc. Although do not include presentation layer concepts like “new entry rows” in the Table Model: these should be done in a separate JTable.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s