JXTable监听排序并以同样的方式对类似的表进行排序

发布于 2024-08-17 16:10:22 字数 92 浏览 2 评论 0原文

我有许多 JXTables,它们都有相同的列(但数据不同)。您可以通过单击其中一列的标题对数据进行排序。我现在想要的是,当单击其中一个表的标题时,其他表以相同的方式排序。

I have a number of JXTables which all have the same columns (but different data). You can sort the data by clicking on one the header of one of the columns. What I want now, is that the other tables are sorted the same way when clicking on the header of one of the table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

余厌 2024-08-24 16:10:23

您可以定义一个中介器类,该类引用每个 JTableRowSorter 并将其自身注册为每个排序器的 RowSorterListener。当给定的排序器发生更改时,您可以使用 getSortKets() 检索其当前的排序键列表,并使用 setSortKeys(List) 将它们传递给每个其他排序器。

示例

首先我们定义中介类:

public class SortMediator implements RowSorterListener {
  private final List<RowSorter> sorters;
  private boolean changing;

  public void addRowSorter(RowSorter sorter) {
    this.sorters.add(sorter);
  }

  public void sorterChanged(RowSorterEvent e) {
    ...
  }
}

现在我们实现 sorterChanged(RowSorterEvent e) 来响应给定的排序器事件:

  public void sorterChanged(RowSorterEvent e) {
    // The changing flag prevents an infinite loop after responding to the inital
    // sort event.
    if (!changing) {
      changing = true;

      RowSorter changedSorter = e.getSource();
      List<? extends SortKey> keys = changedSorter.getKeys();

      for (RowSorter sorter : sorters) {
        if (sorter != changedSorter) {
          // Install new sort keys, which will cause the sorter to re-sort.
          // The changing flag will prevent the mediator from reacting to this.
          sorter.setSortKeys(keys);
        }
      }
    }
  }

You could define a mediator class that references each JTable's RowSorter and registers itself as a RowSorterListener with each sorter. When a given sorter changes you could retrieve its current list of sort keys using getSortKets() and pass them to every other sorter using setSortKeys(List<? extends SortKey>).

Example

First we define the mediator class:

public class SortMediator implements RowSorterListener {
  private final List<RowSorter> sorters;
  private boolean changing;

  public void addRowSorter(RowSorter sorter) {
    this.sorters.add(sorter);
  }

  public void sorterChanged(RowSorterEvent e) {
    ...
  }
}

Now we implement sorterChanged(RowSorterEvent e) to respond to a given sorter event:

  public void sorterChanged(RowSorterEvent e) {
    // The changing flag prevents an infinite loop after responding to the inital
    // sort event.
    if (!changing) {
      changing = true;

      RowSorter changedSorter = e.getSource();
      List<? extends SortKey> keys = changedSorter.getKeys();

      for (RowSorter sorter : sorters) {
        if (sorter != changedSorter) {
          // Install new sort keys, which will cause the sorter to re-sort.
          // The changing flag will prevent the mediator from reacting to this.
          sorter.setSortKeys(keys);
        }
      }
    }
  }
屋檐 2024-08-24 16:10:23

我不会这样做,因为它会夺走用户的控制权:他/她可能希望对表进行不同的排序,以比较不同的数据。

相反,请在“查看”菜单中添加“排序依据”选项。更改此选项将对所有表进行排序,但随后将其保留,除非用户想要对特定表进行排序。

I would not do that, because it takes control away from the user: s/he may want to have the tables sorted differently, to compare different pieces of data.

Instead, add a "Sort By" option to your View menu. Changing this option will sort all tables, but then leave them alone unless the user wants to sort a specific table.

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