Thread: sorting JTable
View Single Post
  #1 (permalink)  
Old 07-17-2007, 04:49 PM
mansi_3001 mansi_3001 is offline
Member
 
Join Date: Jul 2007
Posts: 2
mansi_3001 is on a distinguished road
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..
Reply With Quote
Sponsored Links