how to sort the contents of jtable using column in jdk1.5
How i need a way to do sorting on JTable rows by table columns. what is the gud way to do that. i need the way that jdk1.5 support. not jdk 1.6 ;)
Message was edited by:
JavaHeroPrince
How i need a way to do sorting on JTable rows by table columns. what is the gud way to do that. i need the way that jdk1.5 support. not jdk 1.6 ;)
Message was edited by:
JavaHeroPrince
Read the JTable API where you will find a link to the Swing tutorial on "How to Use Tables" which still contains information of sorting pre JDK6.
Try using the sample code given below.
/**
/**
author@ imran
*/
* Very simplistic table sorter. Sorts the data in table based on first column...
*/
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableSorter {
static DefaultTableModel model = null;
static Comparator tableComparator = null;
static JTable table = null;
static int sortOrder = -1;
static final String SORT = "SORT";
public TableSorter() {
}
/**
* UI Stuff....
*/
private static JTable getTable() {
model = createTable();
table = new JTable(model);
return table;
}
private static DefaultTableModel createTable() {
Object colNames[] = { "Header1", "Header2", "Header3"};
int NUM_ROWS = 6 ;
model = new DefaultTableModel(getData(colNames.length, NUM_ROWS), colNames);
tableComparator = new Mycomparator();
return model;
}
//populate dummy data...
private static Object[][] getData(int rows, int cols) {
Vector data = new Vector();
String obj[][] = null;
obj = new String[rows][cols];
for (int cnt = 0; cnt < rows; cnt++) {
for (int icnt = 0; icnt < cols; icnt++) {
obj[cnt][icnt] = "Data" + cnt + "" + icnt;
} //for icnt
data.add(obj);
} //for cnt
return obj;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Table Sorter");
frame.setLayout(new BorderLayout());
JTable table = getTable();
frame.add(table.getTableHeader(), BorderLayout.NORTH);
frame.add(getBtnPanel(), BorderLayout.SOUTH);
frame.add(table);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static JPanel getBtnPanel() {
JPanel pnl = new JPanel();
JButton btnSort = new JButton(SORT);
btnSort.setActionCommand(SORT);
btnSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand() == SORT) {
//sort the data...
sort(model);
}
}
}
);
pnl.add(btnSort);
return pnl;
}
private static void sort(DefaultTableModel _mdl) {
//here's the cool stuff.. use the collections to sort the table.. .just
//provide the compartor...
//DataVector is the vector which represents the rows...
Collections.sort(_mdl.getDataVector(), tableComparator);
table.repaint(); //after sorting repaint the table...
sortOrder = - (sortOrder); //toggle the sort order...
}
static class Mycomparator
implements Comparator {
//table is a vector of vectors...
public int compare(Object obj1, Object obj2) {
int retValue = sort( (Vector) obj1, (Vector) obj2);
if (null == obj1 || null == obj2) {
return sortOrder;
}
retValue = sortOrder * retValue;
return retValue;
}
public boolean equals(Object obj) {
return true;
}
//here the value is hardcoded for column 0
private int sort(Vector v1, Vector v2) {
//extract the first column for each row...
String val1 = (String) v1.get(0);
String val2 = (String) v2.get(0);
return val1.compareTo(val2);
} //sort
} //class
}