Sorting JTable on more than just the values listed in the columns

Posted on 16th Feb 2014 by admin

I have a collection of objects that store (among other things) two integer values. I only want to display one of these values. The displayed value will dictate the sort order of the JTable.

However, I want to break ties with the other, undisplayed, integer value.

I've gone through the trouble of specifying my own Comparator for the TableRowSorter. However, this method doesn't have access to the row index into the table. It is only passed the values in the column that is being sorted, not their position in the column. Without having a reference back to the row index (which I use as an index to the collection of objects) I cannot get the secondary value, unless I also display this value in its own column and sort on Sort Keys.

I don't want to do that, because I feel it clutters up the look of the table.


For example, I have the two sets of numbers

Row Name | Displayed Value | Undisplayed Value
rowA | 7 | 3
rowB | 7 | 5

With this set of data, rowB should be listed above rowA. Any ideas on how to implement this.

I'm pretty new to Swing, so there could be something simple that I'm missing (hopefully).

Other forums