• 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

jtable sort on multiple columns

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I would like to know if anyone has any full working examples of sorting a jtable on multiple columns. I have the following data items in a table.
When the user clicks on the market column,
the results should be sorted by market, code,sequence like..
market media code sequence
BF TV 7 1
CA LM 2 1
CA LM 5 1
CA LM 5 2
HT TV 1 1
HT TV 1 2
HT TV 1 3
NY OD 3 1
if the user sorts on media, the code should be ordered by media, code, sequence..
market media code sequence
CA LM 2 1
CA LM 5 1
CA LM 5 2
NY OD 3 1
BF TV 7 1
HT TV 1 1
HT TV 1 2
HT TV 1 3
The problem is that I have multiple rows in the table that relate to each other and therfore need to keep them together on any sort that is done.
The rows need to be always sorted first by clicked column and then by code and sequence.
I am using the sort logic from this site as a starter..
http://www2.gol.com/users/tame/swing/examples/JTableExamples5.html
It only allows sorting on one column.

Any help would be appreciated
John.
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
each row should be wrapped in an Object that implements comparable in the way you expect the sorting algorithm in the table to work. that means you can specify the attribute that is first checked on a comparison, than the other attributes in the order you wish. you will have to write your own comparator that takes the attribute as an argument.
in the table model the row objects are placed in a List. Whenever a sort is requested you can do a Collections.sort(List, Comparator) and reload the data into the jtable.
chantal
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic