摇摆:听众执行顺序的自定义组件
题
我的定制组件由一个JTree
内部的三个JPanel
s。只有一个JTree
应一次被选择,所以我添加了一个TreeSelectionListener
到它们中的每上先前选择clearSelection
调用JTree
。
我想其他TreeSelectionListener
s添加到JTree
s是确保选择 - 处理听众总是首先执行。我不希望把一切都在一个单一的TreeSelectionListener
。
我应该怎么办?由于事先!
解决方案
也许你可以通过这样的方式您的听众被依次调用它的下一次添加新的侦听到现有的将事件转发到它的听众把它们连。
// 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 );
}
}
}
其他提示
不是一个很好的解决方案,但你可以在SwingUtilities.invokeLater(...)包代码。这将代码添加到EDT的端部,这意味着其他听者代码已被执行之后,将最终执行。
不隶属于 StackOverflow