Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2007, 03:49 PM
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..
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-17-2007, 07:27 PM
Member
 
Join Date: Jul 2007
Posts: 33
cecily is on a distinguished road
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()); } } } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-18-2007, 08:26 AM
Member
 
Join Date: Jul 2007
Posts: 2
mansi_3001 is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-10-2007, 07:29 PM
Senior Member
 
Join Date: Jun 2007
Posts: 164
Heather is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting problem... mark-mlt New To Java 4 04-17-2008 03:15 PM
sorting problem mcal New To Java 1 02-14-2008 09:13 AM
Sorting JTable (Vectors) Problem ramapple AWT / Swing 5 02-05-2008 09:54 AM
Help with Sorting Program rhm54 New To Java 3 01-25-2008 11:08 PM
Heap Sorting kesav2005 New To Java 1 11-13-2007 05:04 PM


All times are GMT +3. The time now is 04:04 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org