2

In order to sort a JTable I use a TableRowSorter with different Keys which works great. But now I would like to apply this sorter also to the case the user changes the sort direction. Probably I have to do it in toggleSortOrder, but what I have to do here?

sorter = new TableRowSorter<TableModel>(model) {

    @Override
    public void toggleSortOrder(int column) {

        List<SortKey> keys = new ArrayList<SortKey>(sorter.getSortKeys());

        //use sorter

        super.toggleSortOrder(column);
    }
};

List<SortKey> keys = new ArrayList<SortKey>();
SortKey sortKey, sortKey2, sortKey3;


if(sortType == 0) {

    if(sortDirection == 0) {
        sortKey = new SortKey(1, SortOrder.ASCENDING);
        sortKey2 = new SortKey(0, SortOrder.ASCENDING);
        keys.add(sortKey);
        keys.add(sortKey2);
    } else {
        sortKey = new SortKey(1, SortOrder.DESCENDING);
        sortKey2 = new SortKey(0, SortOrder.DESCENDING);
        keys.add(sortKey);
        keys.add(sortKey2);
    }

} else { 

   if(sortDirection == 0) {
       sortKey = new SortKey(2, SortOrder.ASCENDING);
       sortKey2 = new SortKey(1, SortOrder.ASCENDING);
       sortKey3 = new SortKey(0, SortOrder.ASCENDING);
       keys.add(sortKey);
       keys.add(sortKey2);
       keys.add(sortKey3);
   } else {
       sortKey = new SortKey(2, SortOrder.DESCENDING);
       sortKey2 = new SortKey(1, SortOrder.DESCENDING);
       sortKey3 = new SortKey(0, SortOrder.DESCENDING);
       keys.add(sortKey);
       keys.add(sortKey2);
       keys.add(sortKey3);
   }
}

sorter.setRowFilter(null);
sorter.setSortKeys(keys);
sorter.sort();

sorter.setSortsOnUpdates(true);

table.setRowSorter(sorter);
user1703554
  • 119
  • 4
  • 10

2 Answers2

3

I am not sure why all that code for simple JTable sorting order toggle.

See below:

On start up:

enter image description here

After Foo column header clicked twice - once for focus (than triangle showing direction appears) second to change the sort order:

enter image description here

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class Test {

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                Object rows[][] = {{"A", 1}, {"A", 2}, {"B", 1}, {"B", 2}, {"C", 1}, {"C", 2}, {"D", 4}, {"E", 5},
                    {"F", 6}};
                String columns[] = {"Foo", "Bar"};

                TableModel model = new DefaultTableModel(rows, columns) {
                    @Override
                    public Class getColumnClass(int column) {
                        Class returnValue;
                        if ((column >= 0) && (column < getColumnCount())) {
                            returnValue = getValueAt(0, column).getClass();
                        } else {
                            returnValue = Object.class;
                        }
                        return returnValue;
                    }
                };

                JTable table = new JTable(model);

                RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
                table.setRowSorter(sorter);

                table.setPreferredScrollableViewportSize(table.getPreferredSize());
                JScrollPane pane = new JScrollPane(table);

                frame.add(pane);

                frame.pack();
                frame.setVisible(true);

            }
        });
    }

    public static void main(String[] args) {
        new Test();
    }
}

References:

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • thx, but this was not my question. I sort the table by different criteria if the first criterion is the same in several rows. But when I click the table header of a column it is only sorted by this single column. – user1703554 Jun 30 '13 at 15:30
  • @user1703554 I understand you not. Do you want to sort the table data using 2 columns and not just 1 like my above example? – David Kroukamp Jun 30 '13 at 15:33
  • @user1703554 How is that even possible? because than the rows will have mismatched data as columns are all sorted independently? For example take the above code if I sorted *Foo* in ascending order and *Bar* descending order their *pairs* will become mixed as *Foo* - A is *Bar* - 1 but now its *Foo* - A *Bar* - 6 – David Kroukamp Jun 30 '13 at 15:45
  • No, first it's sorted by Foo and if there are e.g. multiple A entries, then it's sorted by Bar: A1 A2 B1 B2 C1 -> A2 A1 B2 B1 C1 in your example. – user1703554 Jun 30 '13 at 15:58
  • @user1703554 I see, but the code I have would do that by default, check my updated post and screen shots – David Kroukamp Jun 30 '13 at 16:27
  • Thx I will accept this. I found another solution and posted it as well. – user1703554 Jun 30 '13 at 16:47
  • @user1703554 [for example](http://stackoverflow.com/a/16664124/714968) answer to your question – mKorbel Jul 01 '13 at 05:41
0

Just for completeness my solution:

            @Override
            public void toggleSortOrder(int column) {

                List<? extends SortKey> sortKeys = getSortKeys();
                List<SortKey> newKeys = new ArrayList<SortKey>();
                if (sortKeys.size() > 0) {
                    if(column == 2) {

                        if (sortKeys.get(0).getSortOrder() == SortOrder.DESCENDING) {

                            newKeys.add(new SortKey(2, SortOrder.ASCENDING));
                            newKeys.add(new SortKey(1, SortOrder.ASCENDING));
                            newKeys.add(new SortKey(0, SortOrder.ASCENDING));

                            table.getRowSorter().setSortKeys(sortKeys);
                        } else {
                            newKeys.add(new SortKey(2, SortOrder.DESCENDING));
                            newKeys.add(new SortKey(1, SortOrder.DESCENDING));
                            newKeys.add(new SortKey(0, SortOrder.DESCENDING));

                            table.getRowSorter().setSortKeys(sortKeys);
                        }

                    } else if(column == 1) {
                        if (sortKeys.get(0).getSortOrder() == SortOrder.DESCENDING) {

                            newKeys.add(new SortKey(1, SortOrder.ASCENDING));
                            newKeys.add(new SortKey(0, SortOrder.ASCENDING));

                            table.getRowSorter().setSortKeys(sortKeys);
                        } else {
                            newKeys.add(new SortKey(1, SortOrder.DESCENDING));
                            newKeys.add(new SortKey(0, SortOrder.DESCENDING));

                            table.getRowSorter().setSortKeys(sortKeys);
                        }

                    }
                }


                sorter.setSortKeys(newKeys);
                    sorter.sort();
          }
user1703554
  • 119
  • 4
  • 10