Results 1 to 7 of 7
  1. #1
    ramapple is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    Default Sorting JTable (Vectors) Problem

    By using the following code:

    Java 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:

    Java 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 08:07 AM. Reason: Clarification

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    See if specifying the type makes any difference:
    Java 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.

  3. #3
    ramapple is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    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? :)

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    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?

  5. #5
    ramapple is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    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.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    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.

  7. #7
    jbt
    jbt is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Default adding rows to JTable after a sort

    Quote 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:
    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();
    It did the trick for me.
    Last edited by jbt; 07-06-2009 at 11:17 PM.

Similar Threads

  1. sorting problem...
    By mark-mlt in forum New To Java
    Replies: 4
    Last Post: 04-17-2008, 02:15 PM
  2. sorting problem
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 08:13 AM
  3. Problem with sorting Table
    By sireesha264 in forum Advanced Java
    Replies: 0
    Last Post: 02-08-2008, 02:21 PM
  4. sorting JTable
    By mansi_3001 in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 06:29 PM
  5. Problem with vectors in java
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •