4

How do I force certain table rows to be at the end of a sorted JTable? For example, if someone clicks on a column heading to sort by that column, I have some rows that I always want to be at the end of the table regardless of the sort order. How would I go about making this happen?

Thanks!

Trippy
  • 131
  • 1
  • 2
  • 7
  • 1
    How do you have your JTable set up? Is there a custom TableModel operating behind the scenes, or do you use a DefaultTable Model? – jpalm Dec 14 '11 at 01:04
  • Well, to create a JTable you need to pass an array of Objects to the constructor, right? If you implement a sorting method so that leaves the final rows unsorted, and then you fire it, you can probably achieve the functionality that you want. – eboix Dec 14 '11 at 01:09
  • It could be solved pretty easily with a RowSorter and a custom comparator. See [here](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting) for some more information. Without some code we can't provide a specific answer. – jpalm Dec 14 '11 at 01:15
  • @jjiceman decided to move the comment into not-an-answer - more comfortable to write and edit :-) – kleopatra Dec 14 '11 at 08:48

2 Answers2

1

You can either implement the Comparator interface, as shown in How to Use Tables: Sorting and Filtering, or implement the Comparable Interface, as shown in the class Value of this example. In either approach, you can alter the result of the comparison method in a way that causes the desired row(s) to appear after all others.

Addendum: As @kleopatra's answer notes, the Comparator must be applied in the context of the table's RowSorter, while an implementation of Comparable would affect the natural ordering. As the result is convoluted and hard to maintain, you may want to look at an alternate approach.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

Beware: this is not an answer, just a kind of summary of the difficulties and some links.

It's one of those tasks which are surprisingly difficult to solve. Nothing in core (not even in SwingX :-) which supports it. In particular, a Comparator doesn't help because it doesn't know anything about the sort direction that is it can't know if it should locate a particular row at the top or at the bottom. The only collaborator which might be able to do so, might be a RowSorter. Due to the fact that everything in DefaultRowSorter is tightly hidden, tweaking boils down to a complete re-implementation. An example to handle nulls had been published by Walter Laan over at OTN and later described to be tweaked for a more complex context.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • +1 for putting this in perspective. I had no idea the implementation would be so troublesome. – trashgod Dec 14 '11 at 22:37
  • @kleopatra please see bug listed (by Andrew Thompson) in http://stackoverflow.com/questions/6187566/problem-formatting-fields-in-a-jtable-differences-between-integer-and-double, sure there is lots for ... – mKorbel Dec 15 '11 at 17:39
  • @mKorbel no, that's unrelated, completely - as in no relation whatever – kleopatra Dec 15 '11 at 22:30