Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-04-2008, 08:43 AM
Member
 
Join Date: Feb 2008
Posts: 7
Rep Power: 0
ramapple is on a distinguished road
Default Sorting JTable (Vectors) Problem
By using the following code:

Code:
Vector rows = new Vector();
Vector cols = new Vector();

DefaultTableModel dtf = new DefaultTableModel();
dtf.setDataVector(rows, cols);

JTable table = new JTable(dtf);
I am able to update the information in the JTable by adding information into the rows vector. However, whenever I sort the JTable using:

Code:
TableRowSorter sorter = new TableRowSorter(table.getModel());
table.setRowSorter(sorter);
It works. But the next time I add information into the rows vector, the information isn't displayed on the JTable. I'm not sure why this is, can someone give me some insight? Thanks!

EDIT: As in, BEFORE I sort the table (under any column), I can add as much data as I want onto the JTable. But after sorting ANY column, the data in the JTable will sort, but I can no longer add any more data onto it.

Last edited by ramapple; 02-04-2008 at 09:07 AM. Reason: Clarification
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-05-2008, 05:03 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
See if specifying the type makes any difference:
Code:
TableRowSorter<DefaultTableModel> sorter =
     new TableRowSorter<DefaultTableModel>(table.getModel());
The idea comes from the next-to-the-last paragraph (just above the warning) in the TableRowSorter api comments section.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-05-2008, 06:06 AM
Member
 
Join Date: Feb 2008
Posts: 7
Rep Power: 0
ramapple is on a distinguished road
Default
Hi,

I've tried to make my code adapt to using that, but still no luck. The table still doesn't seem to display any new data after I sort the columns.

Does anyone have any other ideas?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-05-2008, 07:05 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Next questions:
Are you telling the TableModel that you have made changes to the data?
Is the original Vector the same as the current Vector in the TableModel after the sort, ie, did the sorter change vectors?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-05-2008, 08:23 AM
Member
 
Join Date: Feb 2008
Posts: 7
Rep Power: 0
ramapple is on a distinguished road
Default
Yes, the DefaultTableModel is notified of changes. If not, it would not have displayed information in the first place. I used addNotify() and revalidate() for notifying of its changes.

Also, the vector is indeed the same vector as before the sort. I've run a few debug System.out.println() tests and the vector for the JTable is the same.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-05-2008, 09:54 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
I used addNotify() and revalidate() for notifying of its changes.
There is usually no need for calling addNotify for updating a JTable.
revalidate causes the component to tell its parent to do a new layout of its child components. As mentioned above, this can cause many changes in your app depending on how you put it together; with particular emphasis on your choice of layout manager(s) and use of size hints.
To communicate with your TableModel try methods in the AbstractTableModel class, eg, fireTableDataChanged.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-07-2009, 12:15 AM
jbt jbt is offline
Member
 
Join Date: Jul 2009
Posts: 1
Rep Power: 0
jbt is on a distinguished road
Default adding rows to JTable after a sort
Originally Posted by ramapple View Post
As in, BEFORE I sort the table (under any column), I can add as much data as I want onto the JTable. But after sorting ANY column, the data in the JTable will sort, but I can no longer add any more data onto it.
I had the same problem. I'm using a JTable to display sound files, and I wanted them to be sorted by name, date and size. Before I performed any sort, I was able to add as much files as I wanted, but as soon as I clicked on a column header, the JTable wouldn't allow (or display) any new files that I'd import to it.

The reason for this behavior seems to be that TableRowSorter creates a separate view for your data and that a simple update to the data does not change this view, even after calling fireTableDataChanged().

The trick is to call the method sort() of the TableRowSorter object after a modification to the data has been done.

ex:
Code:
FileTableModel model = new FileTableModel();
 JTable table = new JTable(model);
 TableRowSorter sorter =   new TableRowSorter(table.getModel());
 table.setRowSorter(sorter);

//to call after modification of the data contained in the model
sorter.sort();
table.updateUI();
It did the trick for me.

Last edited by jbt; 07-07-2009 at 12:17 AM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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

BB 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
Problem with sorting Table sireesha264 Advanced Java 0 02-08-2008 03:21 PM
sorting JTable mansi_3001 Advanced Java 3 08-10-2007 07:29 PM
Problem with vectors in java toby New To Java 1 08-07-2007 06:56 AM


All times are GMT +2. The time now is 05:02 PM.



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