Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 02-04-2008, 09:43 AM
Member
 
Join Date: Feb 2008
Posts: 7
ramapple is on a distinguished road
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 10:07 AM. Reason: Clarification
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-05-2008, 06:03 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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, 07:06 AM
Member
 
Join Date: Feb 2008
Posts: 7
ramapple is on a distinguished road
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, 08:05 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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, 09:23 AM
Member
 
Join Date: Feb 2008
Posts: 7
ramapple is on a distinguished road
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, 10:54 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
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
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 04:15 PM
sorting problem mcal New To Java 1 02-14-2008 10:13 AM
Problem with sorting Table sireesha264 Advanced Java 0 02-08-2008 04:21 PM
sorting JTable mansi_3001 Advanced Java 3 08-10-2007 08:29 PM
Problem with vectors in java toby New To Java 1 08-07-2007 07:56 AM


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


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