我的定制组件由一个JTree内部的三个JPanels。只有一个JTree应一次被选择,所以我添加了一个TreeSelectionListener到它们中的每上先前选择clearSelection调用JTree

我想其他TreeSelectionListeners添加到JTrees是确保选择 - 处理听众总是首先执行。我不希望把一切都在一个单一的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的端部,这意味着其他听者代码已被执行之后,将最终执行。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top