同时对两个 JTable 进行排序
我有两个使用同一 TableModel 类的不同对象构建的 JTable。当我单击表 1 中的一列进行排序时,要求其他表 2 也应根据 JTable 1 中单击的同一列进行排序。有什么方法可以找到使用了表 1 中的哪一列或排序基于。使用它,是否有任何方法可以通过表 2 上同一列的任何方法调用来调用排序。
请提供您对任何 java api 的建议或指示。另外,如果有任何带有示例的链接将会有很大帮助。 -保罗。
I have two JTables built with different objects of same TableModel class. When I click on one column in Table 1 to sort, the requirement is that the other Table 2 should also get sorted based on the same column that was clicked on in JTable 1. Is there any way to find what column in Table 1 was used or the sorting was based on. Using that, is there any way to invoke sorting via any method call on Table 2 for the same column.
Please provide your suggestions or pointers to any java apis. Also, if there is any link having an example would be of great help.
-Paul.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方法是监听表排序器的变化,并将第二个表的 sortKey 设置为相同:
如果需要保持双向同步,请向两者注册监听器,并添加一些逻辑,以便在排序时不执行任何操作。排序更改由侦听器触发。
编辑
在写了两次几乎相同的评论(针对建议对模型进行排序的答案)后,决定从
The way to go is to listen to the changes of the table's sorter and set the sortKeys of the second table to the same:
If you need to keep the synch both ways, register the listener to both and add some logic to do nothing when the sort change was triggered by the listener.
Edit
after writing a nearly same comment twice (to the answers of suggesting doing the sorting on the model), decided to add it here
我认为用户界面只会使问题变得复杂。从基础表数据的角度来考虑,你会做得更好。如果你能做到这一点,那么显示问题就很简单了。
对值集合进行排序并不困难。您可以使用 java.util.Collections 和 Comparable 来做到这一点。
诀窍是维护已排序列的索引与行中其余列的索引之间的关联。
I think the user interface only complicates the issue. Think about it in terms of the underlying table data and you'll fare better. If you can get that right, it's a simple matter of display.
Sorting a collection of values isn't difficult. You can use
java.util.Collections
andComparable
to do that.The trick is maintaining the association between the indexes of the sorted column and the rest of the columns in the row.
最简单的方法是对基础数据进行排序。或者您可以实现一个排序侦听器并将其添加到两个表中。
以下线程展示了如何在 JXTable 中实现它:
http://www.java.net/node/680987< /a>
The easiest way is to sort the underlying data. Or you can implement a sortlistener and add it to both tables.
The following thread shows how to implement it in JXTable:
http://www.java.net/node/680987