摇摆:自定义组件上的侦听器执行顺序 [英] Swing: listeners execution order on custom component
问题描述
我的自定义组件由JPanel
中的三个JTree
组成.一次只能选择一个JTree
,因此我向每个添加了TreeSelectionListener
的对象调用了先前选择的JTree
上的clearSelection
.
My custom component is composed of three JTree
s inside a JPanel
. Only one JTree
should be selected at a time, so I've added a TreeSelectionListener
to each of them that calls clearSelection
on the previously selected JTree
.
我想将其他TreeSelectionListener
添加到JTree
中,以确保始终先执行选择处理侦听器.我不想将所有内容都放在一个TreeSelectionListener
中.
I'd like to add other TreeSelectionListener
s to the JTree
s being sure that the selection- handling listeners always get executed first. I'd prefer not to put everything in one single TreeSelectionListener
.
我该怎么办?预先感谢!
What should I do? Thanks in advance!
推荐答案
可能您可以通过将新的侦听器添加到现有侦听器中,从而在下次调用侦听器时将其链接到该对象,从而将它们链接到它的听众.
Probably you could chain them by adding the new listener to the existing one in such a way the next time your listener gets invoked it in turn will forward the event to its listeners.
// This is your current listener implementation
class CustomTreeSelectionListener implements TreeSelectionListener {
// listeners to which the even will be forwarded
private List<TreeSelectionListener> ownLIsteners;
public void addListener( TreeSelectionListener newListener ) {
ownListeners.add( newListener );
}
// add also removeListener( .... )
// TreeSelectionListener interface implementation...
public void valueChanged( TreeSelectionEvent e ) {
process( e ); // do what you do now
// Forward the message.
for( TreeSelectionListener listener : ownListeners ) {
listener.valueChanged( e );
}
}
}
这篇关于摇摆:自定义组件上的侦听器执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!