2016-03-03 33 views
1

我嘗試對JTable進行排序時出現問題。如果我按列的標題,我可以輕鬆地對列進行排序。但是,當輸入包含JTableJFrame時,我想自動對我的JTable的第一列進行排序。我將不勝感激任何幫助。謝謝如何在輸入JFrame時對JTable列進行排序?

回答

4

您可以使用SortKeys來做到這一點。例如

TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel()); 
table.setRowSorter(sorter); 
List<RowSorter.SortKey> sortKeys = new ArrayList<>(); 

int columnIndexToSort = 0; //This is the first column 
sortKeys.add(new RowSorter.SortKey(columnIndexToSort, SortOrder.ASCENDING)); 

sorter.setSortKeys(sortKeys); 
sorter.sort(); 

看一看this websitejava documentation以獲取更多信息。

+1

它的工作原理!非常感謝! – salvo9415

+0

@ salvo9415沒問題。你可以點擊答案旁邊的勾號嗎?謝謝 – Dan

相關問題