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.
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.
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.
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()