同时对两个 JTable 进行排序

发布于 2024-12-10 20:08:59 字数 220 浏览 0 评论 0原文

我有两个使用同一 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓味的萝莉 2024-12-17 20:08:59

解决方法是监听表排序器的变化,并将第二个表的 sortKey 设置为相同:

    RowSorterListener l = new RowSorterListener() {

        @Override
        public void sorterChanged(RowSorterEvent e) {
            if (RowSorterEvent.Type.SORT_ORDER_CHANGED == e.getType()) {
                RowSorter sorter = e.getSource();
                otherTable.getRowSorter().setSortKeys(sorter.getSortKeys());
            }
        }

    };
    table.getRowSorter().addRowSorterListener(l);

如果需要保持双向同步,请向两者注册监听器,并添加一些逻辑,以便在排序时不执行任何操作。排序更改由侦听器触发。

编辑

在写了两次几乎相同的评论(针对建议对模型进行排序的答案)后,决定从

  • 技术上将其添加到此处,排序可以决定是模型或模型的责任查看领域。无论哪种方式都有利有弊(过去曾引起激烈争论)。完成后,在 ui 开发中的任何地方都坚持这一决定,
  • 保持模型和视图坐标系之间的索引映射是隐藏挑战的地方,无论哪种方式,
  • Swing/X 决定将其视为视图责任,塞满任何模型基于顶部的自定义排序/同步正在与系统作斗争

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:

    RowSorterListener l = new RowSorterListener() {

        @Override
        public void sorterChanged(RowSorterEvent e) {
            if (RowSorterEvent.Type.SORT_ORDER_CHANGED == e.getType()) {
                RowSorter sorter = e.getSource();
                otherTable.getRowSorter().setSortKeys(sorter.getSortKeys());
            }
        }

    };
    table.getRowSorter().addRowSorterListener(l);

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

  • technically, sorting can be decided to be the responsibility of either the model or the view realm. There are (strongly debated in the past) pros and cons either way. Once done, stick to that decision everywhere in ui dev
  • keeping the index mapping between model and view coordinate system is where the challenges hides, either way
  • Swing/X decided to regard it a view responsibility, cramming any model-based custom sort/synch on top is fighting the system
醉梦枕江山 2024-12-17 20:08:59

我认为用户界面只会使问题变得复杂。从基础表数据的角度来考虑,你会做得更好。如果你能做到这一点,那么显示问题就很简单了。

对值集合进行排序并不困难。您可以使用 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 and Comparable 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.

风渺 2024-12-17 20:08:59

最简单的方法是对基础数据进行排序。或者您可以实现一个排序侦听器并将其添加到两个表中。

以下线程展示了如何在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文