wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Ordering jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Ordering jtable" Watch "Ordering jtable" New topic
Author

Ordering jtable

Diederick de Vries
Greenhorn

Joined: Jul 14, 2005
Posts: 12
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Diederick de Vries
Greenhorn

Joined: Jul 14, 2005
Posts: 12
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

Joined: Jul 26, 2007
Posts: 10426
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Diederick de Vries
Greenhorn

Joined: Jul 14, 2005
Posts: 12
Maneesh Godbole wrote:You can provide your own Comparator. Check out Collections#sort()


Awesome. That worked. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ordering jtable