摇摆:自定义组件上的侦听器执行顺序 [英] Swing: listeners execution order on custom component

查看:90
本文介绍了摇摆:自定义组件上的侦听器执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义组件由JPanel中的三个JTree组成.一次只能选择一个JTree,因此我向每个添加了TreeSelectionListener的对象调用了先前选择的JTree上的clearSelection.

My custom component is composed of three JTrees 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 TreeSelectionListeners to the JTrees 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆