This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Ordering jtable

 
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!
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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!



 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • 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!
 
I am going to test your electrical conductivity with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic