This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ordering jtable

 
Diederick de Vries
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a JTable with just one column. Every cell contains a picture thumbnail with a name and a timestamp below. The pictures themselves are represented by JPA Entities, which have tags associated to them (a comma separated String, which does not appear in the table). Now I want my user to be able to sort the table based on the timestamp or the tag String. I would like a popup menu that lets the user select the way he want the ordering to take place.

What would be the best way to implement this? I can compare Strings or timestamps, but just making my picture Entity implement Comparable won't do the trick, because that won't let me choose what field should be used to compare the pictures.

Thanks for any help!
 
Rob Spoor
Sheriff
Pie
Posts: 20497
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have one column in which you display an image, a name and a timestamp? Why not use at least two columns, splitting off the timestamp to its own column? With a custom TableModel you can still use one column for storage. A small example:
Now you can use a TableRowSorter for automated sorting.
 
Diederick de Vries
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: Why not use at least two columns, splitting off the timestamp to its own column?


The reason I only use one column is that my table is a thumbnail column to the left of a picture view panel. Whenever a thumbnail is clicked on, the picture view panel will show the picture in full. Using more than one column would take up a lot of space, most of which would be white (the cells would have the height of a thumbnail), which I would rather use for the image view panel. Putting the tags in the table would be ugly and unneeded from the user's perspective and take up a lot of room as well. That is why I want to use a popup menu instead of clickable table headers.

Is there a way to do this?

Thanks!



 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Diederick de Vries wrote:
I can compare Strings or timestamps, but just making my picture Entity implement Comparable won't do the trick, because that won't let me choose what field should be used to compare the pictures.


You can provide your own Comparator. Check out Collections#sort()
 
Diederick de Vries
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:You can provide your own Comparator. Check out Collections#sort()


Awesome. That worked. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic