This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable sorting for JSE 5.0  RSS feed

 
Jof Gronning
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need to sort a JTable and the target platform is JSE 5.0
What is the best approach.
Can I use JSE 6.0 TableRowSorter (is this compatible with 5.0)
or do I have to write the code to do this?

Best,
Jof
 
Pavan Kumar Srinivasan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you will have to decorate the existing model with a SortModel or extend the AbstractModel(First approach is definitely preferred)

Since you brought up the idea of sorting/filtering have a look at
Glazed Lists. It helps to accomplish such tasks very easiy.
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written something similar once that only worked with DefaultTableModel. What it did was create an Object[] for each row, add those to a List, sort the List (using a Comparator<Object[]>, remove all rows and add all rows again but then sorted. Not quite elegant, and I'd say inefficient if the table has many rows.

There was also no visual signaling on the JTableHeader about which column was sorted last.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always build my table models from List(s) of custom objects.
In 1.5 I used to provide a custom Comparator which used to accept the column index and sort directon which were used by the compare method.
I also used "\u2191" and "\u2193" in my table header renderers to provide visual clues on last sorted column and sort direction.
By this approach, once you call Collections.sort() all you need to do is call fireTableDataChanged() to referesh the UI.
[ October 23, 2008: Message edited by: Maneesh Godbole ]
 
Jof Gronning
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and thanks all,
This is excellent. I also found some code called TableSorter that uses the Decorator pattern.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!