Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting JTable Columns

 
Dennis Putnam
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic