This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes A question about TableRowSorter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A question about TableRowSorter" Watch "A question about TableRowSorter" New topic

A question about TableRowSorter

Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 193
I tried using TableRowSorter for a table. It is mostly easy to set up-

but it has a major failing- when the table has empty rows, they appear at the top of the table. I naively thought that I could provide a custom comparator

No such luck, the code for TableRowSorter does not use the custom comparator for null fields.

I tried a crude workaround, I reversed the order of my comparator,

so there is at least a way to get the numbers in the order I want and the blank rows at the bottom. But it would be nicer if the arrows were consistent with the sort.

Are there better other alternates than copying the TableRowSort class so I can change the two offending lines?
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 7502

Jon Swanson wrote:No such luck, the code for TableRowSorter does not use the custom comparator for null fields.

I'm afraid I'm no GUI expert, but that does make sense to me; otherwise you're likely to get NPE's cropping up in deeply embedded code.

I guess my question is: why are these rows (or column fields) null?


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 193
Sorting the rows was added as a requirement after I finished coding something up that allowed a user to interactively enter data into a table. I originally set things up display a table with the number of rows that were likely to be needed. Then, if the user needed more, they could add (or delete) rows from a popup menu. So there will typically be at least one empty row for adding the next piece of data.

I also notice once TableRowSorter is active, it sorts automatically when I add a row, which is not quite what the user is expecting (i.e. they might want to insert a row, enter data, then sort). So there are a few other tricks I may need to learn about TableRowSorter. I'm also looking to have the table initially display in 'sort' mode as if the user had clicked on the table header. I think that might be doable, still experimenting on that.
jQuery in Action, 2nd edition
subject: A question about TableRowSorter
Similar Threads
TableSorter and Selection gives problems
Sorting JTable with scientific notation doubles in Win32
JTable question....again!
Sorting Collection
table sorter