2016-03-03 53 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