• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Ordering jtable

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!



 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:You can provide your own Comparator. Check out Collections#sort()



Awesome. That worked. Thanks!
 
This one time, at bandcamp, I had relations with a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic