java JTable中的多列排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/110016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:03:45  来源:igfitidea点击:

Multiple column sort in JTable

javaswingjtable

提问by Chris Jester-Young

I know that JTablecan 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可以按单列排序。但是是否可以允许多列排序还是我需要自己编写代码?

回答by Chris Jester-Young

You can sort by multiple columns by specifying more than one sort key when calling setSortKeysin the RowSorteryou're using.

你可以通过调用时指定多个排序键排序多个列setSortKeysRowSorter你使用。

回答by Joshua

You should be able to set the TableRowSorter and the Comparator associated with it. Example:

您应该能够设置 TableRowSorter 和与之关联的 Comparator。例子:

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));

回答by David Koelle

Look into JXTable. JXTable is an extension of JTable that supports multi-column sorting, as well as other functions that JTable doesn't provide. It's freely available from JDNC / SwingLabs.

查看JXTable。JXTable 是 JTable 的扩展,支持多列排序,以及 JTable 不提供的其他功能。它可以从 JDNC / SwingLabs 免费获得。

回答by David Koelle

ETable from the netbeans collection.
It is part of 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

来自 netbeans 集合的 ETable。
它是 org-netbeans-swing-outline.jar
的一部分 谷歌搜索可以打开它。ETable 主要是 Outline(一个 TreeTable)的基础,但它具有内置的多列排序以及许多其他不错的功能

回答by Ram Dutt Shukla

"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 绑定属性设置为 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 的一个实例。