Jong
Jong

Reputation: 131

Jtable sort by column

JTable

Header1 | Header2 | Header3

temp1 | temp1 | Boolean.FALSE

temp2 | temp2 | Boolean.TRUE

temp3 | temp3 | Boolean.FALSE

temp4 | temp4 | Boolean.TRUE

How can I sort Header3? into Boolean.TRUE first then Boolean.FALSE.

Upvotes: 0

Views: 127

Answers (2)

trashgod
trashgod

Reputation: 205875

I don't know to set the third column as the one to be sorted.

Referring to the tutorial and this example, you can specify the column and SortOrder like this for CHECK_COL:

public CheckABunch() {
    ...
    table.setAutoCreateRowSorter(true);
    DefaultRowSorter<DefaultTableModel, Integer> sorter =
        ((DefaultRowSorter) table.getRowSorter());
    List<RowSorter.SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
    sortKeys.add(new RowSorter.SortKey(CHECK_COL, SortOrder.DESCENDING));
    sorter.setSortKeys(sortKeys);
}

enter image description here

Upvotes: 2

Addict
Addict

Reputation: 823

You can use TableRowSorter for sorting. Search for it, you will get plenty of examples.

Upvotes: 0

Related Questions