-
sorting JTable
public class MyTableApp extends JFrame {
JTable myTable;
JButton update;
JButton insert;
JButton delete;
JPanel p,mainPanel;
MyTabModel tm;
DefaultTableModel tabModel;
JScrollPane myPane;
Vector rows,columns;
protected JLabel titleLabel=new JLabel("Click table header to sort the column");
//constructor
MyTableApp(/*Connection con,String tableName*/){
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf. windows.WindowsLookAndFee l");
}
catch(Exception e){
System.out.println("Error on look and feel");
}
tm = new MyTabModel(/*con,tableName*/);
//TableSorterDemo ts=new TableSorterDemo();
myTable = new JTable(tm);
/*MyTabModel myModel = new MyTabModel();*/
TableSorter sorter = new TableSorter(tm);
sorter.addMouseListenerToHeaderInTable(myTable);
myTable.setModel(tm);
update = new JButton("Update/Save");
insert = new JButton("Add");
delete = new JButton("Delete(one at a time)");
p = new JPanel();
p.add(insert);
p.add(delete);
p.add(update);
myPane = new JScrollPane(myTable,JScrollPane.VERTICAL_SCROLLBAR _AS_NEEDED,JScrollPane.HO RIZONTAL_SCROLLBAR_AS_NEEDED);
// myTable.setSelectionForeground(Color.white);
// myTable.setSelectionBackground(Color.red);
//myTable.setSelectionMode(ListSelectionModel.SINGLE _SELECTION);
//getContentPane().add(myPane);
JTableHeader header = myTable.getTableHeader();
header.setUpdateTableInRealTime(true);
//header.addMouseListener(tm.new MouseListener());
header.setReorderingAllowed(true);
mainPanel=new JPanel();
this.setSize(800,600);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainPanel.setLayout(new BorderLayout());
mainPanel.add("Center",myPane);
mainPanel.add("South",p);
mainPanel.setBackground(Color.white);
p.setBackground(Color.white);
myTable.getParent().setBackground(Color.white);
this.getContentPane().add(mainPanel);
this.setVisible(true);
I am using class MyTableApp to create Jtable whereas the data is being populated from database using another class MyTabModel.i want to sort the columns in the JTable hence using TableSorter and TableMap from examples by sun.Now the problem is that instead of including the mouselistener in MyTableApp class above im nt able to sort the JTable.please help...its urgent..
-
This example implements a method that sorts all the rows in a DefaultTableModel based on the values of the column.
Code:
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
// Add data here...
// Disable autoCreateColumnsFromModel otherwise all the column customizations
// and adjustments will be lost when the model data is sorted
table.setAutoCreateColumnsFromModel(false);
// Sort all the rows in descending order based on the
// values in the second column of the model
sortAllRowsBy(model, 1, false);
// Regardless of sort order (ascending or descending), null values always appear last.
// colIndex specifies a column in model.
public void sortAllRowsBy(DefaultTableModel model, int colIndex, boolean ascending) {
Vector data = model.getDataVector();
Collections.sort(data, new ColumnSorter(colIndex, ascending));
model.fireTableStructureChanged();
}
// This comparator is used to sort vectors of data
public class ColumnSorter implements Comparator {
int colIndex;
boolean ascending;
ColumnSorter(int colIndex, boolean ascending) {
this.colIndex = colIndex;
this.ascending = ascending;
}
public int compare(Object a, Object b) {
Vector v1 = (Vector)a;
Vector v2 = (Vector)b;
Object o1 = v1.get(colIndex);
Object o2 = v2.get(colIndex);
// Treat empty strains like nulls
if (o1 instanceof String && ((String)o1).length() == 0) {
o1 = null;
}
if (o2 instanceof String && ((String)o2).length() == 0) {
o2 = null;
}
// Sort nulls so they appear last, regardless
// of sort order
if (o1 == null && o2 == null) {
return 0;
} else if (o1 == null) {
return 1;
} else if (o2 == null) {
return -1;
} else if (o1 instanceof Comparable) {
if (ascending) {
return ((Comparable)o1).compareTo(o2);
} else {
return ((Comparable)o2).compareTo(o1);
}
} else {
if (ascending) {
return o1.toString().compareTo(o2.toString());
} else {
return o2.toString().compareTo(o1.toString());
}
}
}
}
-
hiii...i have already done this..i.e.i hav used the examples from sun java.TableSorter is the class which uses TableMap.as such the demo at sun is workingfine but whn i trid to implement that in my model then sorting is not taking place.i hav implemented the sorting in MyTableApp and the class MyTabModel is extending the AbstractTableModel.but problem is still not resolved
myTable = new JTable(tm);
/*MyTabModel myModel = new MyTabModel();*/
TableSorter sorter = new TableSorter(tm);
sorter.addMouseListenerToHeaderInTable(myTable);
-
this code works
check it out and adapt it to your code
Code:
jlFond = new JLabel();
jlFond.setBackground(Color.white);
JMenuBar mb2 = new JMenuBar();
mb2.add(Box.createHorizontalGlue());
mb2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
ImageIcon middleButtonIcon = new ImageIcon("Iconos/door.gif");
btnExit= new JButton(middleButtonIcon);
btnExit.addActionListener(this);
ImageIcon middleButtonIcon1 = new ImageIcon("Iconos/graTamb (Custom).GIF");
btnGraphics = new JButton(middleButtonIcon1);
btnGraphics .addActionListener(this);
mb2.setBackground(Color.orange);
mb2.add(btnExit);
mb2.add(btnGraphics );
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBackground(Color.white);
panel.add(mb2);
panel.add(jlFond);
desc = new JDesktopPane();
// desc.setLayout(new BorderLayout());
// desc.setLayout(new FlowLayout(FlowLayout.CENTER));
desc.setBackground(Color.white);
this.getContentPane().add(panel);