在 RowSort 操作后执行操作

发布于 2024-07-23 08:23:20 字数 720 浏览 2 评论 0原文

我有一个 JTable 和一个 TableRowSorter,我想在排序完成后执行操作。 我一直在浏览网络,但到目前为止我还没有什么运气。

最初我认为只需一个 RowSorterListener 就可以解决问题,但不幸的是它在排序完成后不会执行该操作。

MouseListener 添加到 JTable 标头可能会起作用,但该解决方案并不是非常优雅。

有人有什么想法吗?

非常感谢!


编辑(来自评论):以下内容添加到扩展AbstractTableModel的自定义TableModel类内的方法中。 每当在自定义 TableModel 类中设置/指定 JTable 时,都会调用此方法。

sorter.addRowSorterListener(new RowSorterListener() {
    @Override public void sorterChanged(RowSorterEvent rowsorterevent) {
        rebuildMItems(); // The method which executes
    }
});

I have a JTable and a TableRowSorter which I'd like to perform an operation after a sort is finished. I've been browsing the net, and so far I haven't had much luck.

Initially I thought just a RowSorterListener would do the trick, but unfortunately it doesn't perform the operation after the sort is finished.

Adding a MouseListener to the JTable header could work, but the solution isn't terribly elegant.

Does anyone have any ideas?

Thanks a bunch!


Edit (from comment): The following is added in a method inside a custom TableModel class which extends AbstractTableModel. This method is invoked whenever the JTable is set/specified in the custom TableModel class.

sorter.addRowSorterListener(new RowSorterListener() {
    @Override public void sorterChanged(RowSorterEvent rowsorterevent) {
        rebuildMItems(); // The method which executes
    }
});

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

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

发布评论

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

评论(1

往日 2024-07-30 08:23:20

两种可能性:

  1. 我看到您有一个自定义的RowSorter。 您不能简单地在 sort() 方法末尾添加对操作的调用吗?

    换句话说,您可以添加以下内容:

    <前><代码>@Override
    公共无效排序(){
    超级排序();
    doSomethingAfterSortingIsDone();
    }

    到您的排序器?

  2. 您当前的方法(在 RowSorterListener 中执行)会执行两次操作:一次用于 SORT_ORDER_CHANGED,一次用于 SORTED。 您可以检查事件的 类型 并且仅在正确的时间执行该操作?

Two possibilities:

  1. I see you have a custom RowSorter. Couldn't you simply add a call to your operation at the end of the sort() method?

    In other words, can you add this:

    @Override
    public void sort() {
        super.sort();
        doSomethingAfterSortingIsDone();
    }
    

    to your sorter?

  2. Your current method (doing it in a RowSorterListener) performs the operation twice: once for SORT_ORDER_CHANGED and once for SORTED. Can you check the event's type and only perform the operation at the correct time?

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