aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Sorting JTable Columns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Sorting JTable Columns" Watch "Sorting JTable Columns" New topic
Author

Sorting JTable Columns

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I need to set up my columns to be sortable but all the examples I've found seem to be exceedingly complex (is it really this hard?). I finally ran across one that seems reasonably simple but the author left out the most important part, the sort. Here is the code segment that is part of my JTable definition:

I don't know what the author had in mind with the sortColumn method, particularly when passing the table model. Can some one spot what was intended in that method or point me to an example of sorting columns given the column number and the table model? TIA.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

No, it's really not that hard. You just got hold of an out-of-date tutorial, that's all. Here's a link to Oracle's tutorial about how to sort and filter tables.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
Wow! I'll say not. I don't understand why my searches did not turn that up? Because it is too simple? Anyway now I want to exclude certain columns from being sorted when clicked. Is that possible? Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

Dennis Putnam wrote:Wow! I'll say not. I don't understand why my searches did not turn that up? Because it is too simple?


No, because it's a Java 7 feature. Before Java 7 came out, it was a lot harder. And so there are tutorials out there which showed how to do it. And since those tutorials are older and have more pages linking to them, they show up better in Google searches.

Anyway now I want to exclude certain columns from being sorted when clicked. Is that possible? Thanks.


Good question. The tutorial doesn't specifically mention that possibility, but it links to the API docs for the TableRowSorter class. And if you poke around there for a bit, you'll see (or at least I saw) that TableRowSorter extends DefaultRowSorter, which has a "boolean isSortable(int)" method to specify which columns can be sorted on. So when you write your TableRowSorter class, just override that method and have it return true or false depending on whether you want the column to be sorted or not.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I am well into noob territory here. I had not intended to write a TableRowSorter class but I take it from your reply that I need one. So here is what I gleaned from the docs:

Unfortunately this produces an "IndexOutOfBounds" exception. This occurs even before my JFrame is displayed.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 217
I figured it out. It was totally unclear from the docs and did not produce a syntax error but the table model instance needed to be passed:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting JTable Columns