Results 1 to 7 of 7
  1. #1
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default Auto sorting JTable by second column

    Hello all

    I am building a program that displays files and folders from a specific folder on my drive using a JTable component. I have created a custom renderer to show jlabel components with images and it's all working great. I also created a custom comparator class that does exactly what i want when i press on the table header. The issue is that i cannot find how to simulate a user clicking on the second column. It always defaults to the first one and i've been searching for days for a solution. Here's one i found on the oracle page and even that doesn't work :

    TableRowSorter sorter = new TableRowSorter(getModel());
    Comparator comp = new FolderTableSorter(false);
    sorter.setComparator(0, comp);
    sorter.setComparator(1, comp);
    sorter.setComparator(2, comp);
    List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
    sortKeys.add(new RowSorter.SortKey(1, SortOrder.DESCENDING));
    sorter.setSortKeys(sortKeys);
    sorter.toggleSortOrder(1);
    setRowSorter(sorter);
    ((FolderTableModel)getModel()).fireTableDataChange d();
    revalidate();

    As you can see i tried all sorts of methods that i thought MIGHT update the table lol. The odd thing is calling togglesortorder actually puts a little triangle in the column header but it doesn't update the table. By the way, this is all hapenning in a JTable subclass with internal nested classes for the renderer and what not.

    Any help would be appreciated!

    Martin

  2. #2
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    bump, Anyone?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Posting a SSCCE might help. If the problem is with the sorting a small table with strings should be enough.

  4. #4
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    I need a custom renderer to display comboboxes, images, buttons and all kinds of stuff in the cells. I'm doing something on the scale of explorer basically.

  5. #5
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    I'm not sure what more you want besides this code example. The table sorting is working perfectly fine except it sorts it by default by the first column and i want it to sort the second column by default. Manually pressing the column header works great. I would just like to simlulate someone pressing the second table header upon startup.

  6. #6
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default

    So nobody knows how to simulate clicking on the second column header without clicking on it?

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

    Default

    Quote Originally Posted by d3n1s View Post
    I'm not sure what more you want besides this code example. The table sorting is working perfectly fine except it sorts it by default by the first column and i want it to sort the second column by default. Manually pressing the column header works great. I would just like to simlulate someone pressing the second table header upon startup.
    Read the link he provided and you'll know what he and many others here want.

Similar Threads

  1. JTable: Disable sorting on one column
    By b.newworld@gmail.com in forum AWT / Swing
    Replies: 1
    Last Post: 09-16-2010, 12:43 AM
  2. Replies: 1
    Last Post: 07-28-2010, 03:46 PM
  3. Sorting in JTable without clicking on column headers
    By ProgrammingPup in forum Advanced Java
    Replies: 6
    Last Post: 11-04-2009, 05:26 AM
  4. Auto updation of Editable Column Indexes
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 0
    Last Post: 10-23-2008, 10:23 AM
  5. Sorting a SWT table by column
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 03:07 PM

Posting Permissions

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