Results 1 to 7 of 7
Thread: Sorting JTable (Vectors) Problem
- 02-04-2008, 07:43 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 7
- Rep Power
- 0
Sorting JTable (Vectors) Problem
By using the following code:
I am able to update the information in the JTable by adding information into the rows vector. However, whenever I sort the JTable using:Java Code:Vector rows = new Vector(); Vector cols = new Vector(); DefaultTableModel dtf = new DefaultTableModel(); dtf.setDataVector(rows, cols); JTable table = new JTable(dtf);
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!Java Code:TableRowSorter sorter = new TableRowSorter(table.getModel()); table.setRowSorter(sorter);
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 08:07 AM. Reason: Clarification
- 02-05-2008, 04:03 AM #2
See if specifying the type makes any difference:
The idea comes from the next-to-the-last paragraph (just above the warning) in the TableRowSorter api comments section.Java Code:TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(table.getModel());
- 02-05-2008, 05:06 AM #3
Member
- Join Date
- Feb 2008
- Posts
- 7
- Rep Power
- 0
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? :)
- 02-05-2008, 06:05 AM #4
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?
- 02-05-2008, 07:23 AM #5
Member
- Join Date
- Feb 2008
- Posts
- 7
- Rep Power
- 0
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.
- 02-05-2008, 08:54 AM #6
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.
- 07-06-2009, 11:15 PM #7
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
adding rows to JTable after a sort
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:
It did the trick for me.Java 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();
Last edited by jbt; 07-06-2009 at 11:17 PM.
Similar Threads
-
sorting problem...
By mark-mlt in forum New To JavaReplies: 4Last Post: 04-17-2008, 02:15 PM -
sorting problem
By mcal in forum New To JavaReplies: 1Last Post: 02-14-2008, 08:13 AM -
Problem with sorting Table
By sireesha264 in forum Advanced JavaReplies: 0Last Post: 02-08-2008, 02:21 PM -
sorting JTable
By mansi_3001 in forum Advanced JavaReplies: 3Last Post: 08-10-2007, 06:29 PM -
Problem with vectors in java
By toby in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks