2012-11-22 22 views
0

JTableJtable按列分類

Header1 | Header2 | Header3

temp1 | temp1 | Boolean.FALSE

temp2 | temp2 | Boolean.TRUE

temp3 | temp3 | Boolean.FALSE

temp4 | temp4 | Boolean.TRUE

如何對Header3進行排序?首先進入Boolean.TRUE,然後進入Boolean.FALSE。

+0

我可以知道你爲什麼要這麼做? – Amarnath

回答

0

您可以使用TableRowSorter進行排序。搜索它,你會得到很多的例子。

+0

我已經嘗試過了,但我不知道將第三列設置爲要排序的列。 – Jong

2

我不知道將第三列設置爲要排序的列。

參考tutorialexample,您可以指定像這樣CHECK_COL列和SortOrder

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