Hello World,
I have a class which sorts the table, on a column where user double clicks. This works fine. But now what I want instead of single column selection there will be some gui from which user can select multiple columns and table should get sorted on all those column.
Can anybody help me out how to do the sorting on multiple columns? I would appriciate if some one send me code.
Regards,
Sachin Dare.
Here is my existing code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.text.*;
public class Frame1 extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JTable jTable1 ;
Object header[] = {"Jan","Feb","Mar","Apr"};
Object[][] data = {
{new Integer(14), new Integer(12), new Integer(134), new Integer(12) },
{new Integer(5), new Integer(3), new Integer(7), new Integer(2) },
{new Integer(8), new Integer(12), new Integer(123), new Integer(34)},
{new Integer(0), new Integer(0), new Integer(1), new Integer(2)}
};
public Frame1() {
super();
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
DefaultTableModel model = new DefaultTableModel(data,header);
SortFilterModel sortModel = new SortFilterModel(model);
jTable1 = new JTable(sortModel);
sortModel.addMouseListener(jTable1);
jTable1.setCellSelectionEnabled(true);
jTable1.setBackground(Color.pink);
jTable1.setAutoCreateColumnsFromModel(false);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(jTable1);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEE DED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED) ;
this.setTitle("Sort Table.");
getContentPane().setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setBackground(Color.white);
getContentPane().add(scrollPane, BorderLayout.CENTER);
}
public static void main(
String args[]) {
Frame1 myframe=new Frame1();
myframe.setSize(new Dimension(250,250));
myframe.setVisible(true);
}
}
class SortFilterModel extends AbstractTableModel {
private TableModel model;
private int sortColumn;
private Row[] rows;
public SortFilterModel(TableModel m) {
model = m;
int rowCount = model.getRowCount();
rows = new Row[rowCount];
for(int i=0;i<rows.length;i++) {
rows = new Row();
rows.index = i;
}
}
public void sort(int c) {
sortColumn = c;
Arrays.sort(rows);
fireTableDataChanged();
}
public void addMouseListener(final JTable table) {
table.getTableHeader().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
int clickCount = me.getClickCount();
if(clickCount == 2) {
System.out.println("Clicked.......");
int tableColumn = table.columnAtPoint(me.getPoint());
int modelColumn = table.convertColumnIndexToModel(tableColumn);
sort(modelColumn);
}
}
});
}
public int getRowCount() {
return(model.getRowCount());
}
public int getColumnCount() {
return(model.getColumnCount());
}
public Object getValueAt(int row, int column) {
return(model.getValueAt(rows[row].index, column));
}
public void setValueAt(Object value, int row, int column) {
model.setValueAt(value, row, column);
}
public boolean isCellEditable(int r, int c) {
return false;
}
public String getColumnName(int c) {
return model.getColumnName(c);
}
public Class getColumnClass(int c) {
return model.getColumnClass(c);
}
private class Row implements Comparable {
public int index;
private int sortResult;
public int compareTo(Object other) {
try {
Row otherRow = (Row)other;
Object a = model.getValueAt(index, sortColumn);
Object b = model.getValueAt(otherRow.index, sortColumn);
if(a instanceof Comparable) {
sortResult = ((Comparable)a).compareTo(b);
} else {
sortResult = index - otherRow.index;
}
} catch(Exception e) {
e.printStackTrace();
}
return sortResult;
}
}
}