简体   繁体   English

JTable中的多列排序

[英]Multiple column sort in JTable

I know that JTable can sort by a single column. 我知道JTable可以按单列排序。 But is it possible to allow for multiple column sort or do I need to write the code myself? 但是是否可以允许多列排序或者我是否需要自己编写代码?

在您正在使用的RowSorter调用setSortKeys ,可以通过指定多个排序键来按多列排序。

You should be able to set the TableRowSorter and the Comparator associated with it. 您应该能够设置TableRowSorter和与之关联的Comparator。 Example: 例:

TableModel myModel = createMyTableModel();
JTable table = new JTable(myModel);
TableRowSorter t = new TableRowSorter(myModel);
t.setComparator(column that the comparator works against, Comparator<?> comparator);
table.setRowSorter(new TableRowSorter(myModel));

Look into JXTable . 看看JXTable JXTable is an extension of JTable that supports multi-column sorting, as well as other functions that JTable doesn't provide. JXTable是JTable的扩展,支持多列排序,以及JTable不提供的其他功能。 It's freely available from JDNC / SwingLabs. 它可以从JDNC / SwingLabs免费获得。

ETable from the netbeans collection. 来自netbeans集合的ETable。
It is part of org-netbeans-swing-outline.jar 它是org-netbeans-swing-outline.jar的一部分
A google search aught to turn it up. 一个谷歌搜索应该打开它。 The ETable is primarily a foundation for Outline (a TreeTable) but it has multi-column ordering built in as well as many other nice features ETable主要是Outline(一个TreeTable)的基础,但它内置了多列排序以及许多其他不错的功能

"I know that Jtable can sort by a single column. But is it possible to allow for multiple column sort or do i need to write the code myself? " “我知道Jtable可以按单列排序。但是是否可以允许多列排序或者我是否需要自己编写代码?”

Table sorting and filtering is managed by a sorter object. 表排序和过滤由分拣机对象管理。 The easiest way to provide a sorter object is to set autoCreateRowSorter bound property to true; 提供分拣器对象的最简单方法是将autoCreateRowSorter bound属性设置为true;

JTable table = new JTable();
table.setAutoCreateRowSorter(true);

This action defines a row sorter that is an instance of javax.swing.table.TableRowSorter. 此操作定义行排序器,它是javax.swing.table.TableRowSorter的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM