在RowSort操作之后执行操作 [英] Performing an action after a RowSort operation
问题描述
我有一个JTable
和TableRowSorter
,我想在完成排序后在 上执行操作.我一直在浏览网络,到目前为止,我还没有碰到太多运气.
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.
最初,我以为只有RowSorterListener
可以解决问题,但不幸的是,排序完成后它没有执行操作.
Initially I thought just a RowSorterListener
would do the trick, but unfortunately it doesn't perform the operation after the sort is finished.
在JTable
标头中添加MouseListener
可能有效,但是解决方案并不十分优雅.
Adding a MouseListener
to the JTable
header could work, but the solution isn't terribly elegant.
有人有什么想法吗?
感谢一堆!
编辑(来自注释):在自定义TableModel
类中扩展了AbstractTableModel
的方法中添加了以下内容.在自定义TableModel
类中设置/指定JTable
时,将调用此方法.
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
}
});
推荐答案
两种可能性:
-
我看到您有一个自定义的
RowSorter
.您不能简单地在sort()
方法的末尾添加对您的操作的调用吗?
I see you have a custom
RowSorter
. Couldn't you simply add a call to your operation at the end of thesort()
method?
换句话说,您可以添加此内容吗?
In other words, can you add this:
@Override
public void sort() {
super.sort();
doSomethingAfterSortingIsDone();
}
到您的分拣机?
您当前的方法(在RowSorterListener
中执行)执行两次操作:一次用于SORT_ORDER_CHANGED
,一次用于SORTED
.您可以检查事件的类型仅在正确的时间执行操作?
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?
这篇关于在RowSort操作之后执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!