Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default Sorting in JTable without clicking on column headers

    Does anyone know if (and how) one can sort a JTable without requiring the user to click on Column headers.

    I would like them to click a sort button.

    I cant seem to find a fireSort type of method in TableRowSorter.


    Any ideas?

    Thank you.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    When the button is pressed then simply sort the data that is contained in that table and put it back in the table model.
    My Hobby Project: LegacyClone

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    How about if you implement your own custom TableModel (extend the stanard one) ? such as when the JTable was loaded, set it's tableModel to your own one, and have a reference to the table model visible to the button's action handler. Where in the custom table model you would have the reorder the data according to the new sort rule.

  4. #4
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by mrmatt1111 View Post
    When the button is pressed then simply sort the data that is contained in that table and put it back in the table model.
    Thanks i might be able to implement that. After i sort the data and put it back do i have to "update" the table that is displayed? how do i put the data "back" and how do i "update" if i have to?

    Thank you!



    How about if you implement your own custom TableModel (extend the stanard one) ? such as when the JTable was loaded, set it's tableModel to your own one, and have a reference to the table model visible to the button's action handler. Where in the custom table model you would have the reorder the data according to the new sort rule.

    If the above does not work ill try that, good thinking!!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't think that this is an either/or situation. You should sort your data as suggested by mrmatt and create your own table model to hold this data as suggested by pharaoh travishein. Often the best class to extend here is the DefaultTableModel class. Best of luck.

  6. #6
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I don't think that this is an either/or situation. You should sort your data as suggested by mrmatt and create your own table model to hold this data as suggested by pharaoh travishein. Often the best class to extend here is the DefaultTableModel class. Best of luck.
    Hmm...thank you that does make alot of sense. I will come back here if I have more difficulties. Thank you all!!

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    I would like them to click a sort button.
    Not sure I understand the requirement. Once they click the button to sort the data, how do they unsort the data?

    I cant seem to find a fireSort type of method in TableRowSorter.
    You can use the following, but the user can still click on the header to sort as well.

    Java Code:
    table.setAutoCreateRowSorter(true);
    DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter());
    ArrayList list = new ArrayList();
    list.add( new RowSorter.SortKey(0, SortOrder.ASCENDING) );
    sorter.setSortKeys(list);
    sorter.sort();
    I know you can disable sorting on specific columns. I don't know if that applies to just clicking on the header or by sorting manually as well.

Similar Threads

  1. How to span column headers over several columns
    By thayalan in forum AWT / Swing
    Replies: 0
    Last Post: 06-02-2009, 04:33 PM
  2. JCheckBox in JTable column
    By hind in forum New To Java
    Replies: 8
    Last Post: 01-04-2009, 07:40 PM
  3. tooltips for JTable column headers
    By fossildoc in forum AWT / Swing
    Replies: 2
    Last Post: 12-18-2008, 11:42 AM
  4. Sorting a SWT table by column
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 03:07 PM
  5. How to sort column in JTable
    By johnt in forum AWT / Swing
    Replies: 3
    Last Post: 06-14-2008, 05:48 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
  •