• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable sorting in 1.5 using AbstractTableModel

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a JTable with 7 columns, that uses AbstractTableModel. The Jtable is updated with contents from xml file. I have heard 1.6 provides classes that makes sorting easier, but I need to implement this using 1.5. Could any one provide a sample code for this requirement.

Thanks,
Arunagiri

javascript:emoticon('');
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've done something with DefaultTableModel that basically did the following:
- create an Object[] for each row (using getColumnCount() and getValueAt())
- put those Object[]s into a List<Object[]>
- sort that List<Object[]> using a custom Comparator<Object[]>
- clear the table model using setRowCount(0)
- add the rows again

The comparator could sort on multiple columns in any order, having an optional Comparator<Object> for each column. The sorting was triggered by mouse clicks on the JTableHeader. I made this class deprecated once TableRowSorter became active though.


I'm sure you could use a similar approach.
reply
    Bookmark Topic Watch Topic
  • New Topic