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();

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

//for row indices

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.


Leave a Reply

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

You are commenting using your 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