aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable sorting in 1.5 using AbstractTableModel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable sorting in 1.5 using AbstractTableModel" Watch "JTable sorting in 1.5 using AbstractTableModel" New topic
Author

JTable sorting in 1.5 using AbstractTableModel

Arunagiri Rajasekaran
Greenhorn

Joined: Oct 07, 2008
Posts: 2
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('');
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable sorting in 1.5 using AbstractTableModel