java 为树中的节点添加动作侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7023186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:19:18  来源:igfitidea点击:

adding action listener for the node in a tree

javaswingeventsuser-interfacejtree

提问by Suhail Gupta

I am unable to add action listener to a particular node in the tree. This is a tree that i've constructed :

我无法将动作侦听器添加到树中的特定节点。这是我构建的一棵树:

enter image description here

在此处输入图片说明

I want to register a separate listener for each node . Now i have registered a listener on JTree. so, whenever i click on any portion of treethe listener method starts it's work. (i.e now i have a common listener )What i want is when i click on audioa listener registered to hear audio click, should start it's work and the same goes for video. How can i do that ?

我想为每个节点注册一个单独的侦听器。现在我已经注册了一个监听器JTree。因此,每当我单击tree侦听器方法的任何部分时,它都会开始工作。(即现在我有一个共同的听众)我想要的是当我点击音频时注册的听众可以听到音频点击,应该开始它的工作,视频也是如此。我怎样才能做到这一点 ?

This is how i have registered so far :

这是我到目前为止注册的方式:

jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
        public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
            jTree1ValueChanged(evt);
        }
    });
public void jTree1ValueChanged( TreeSelectionEvent tse ) {...}

回答by oliholz

What about this. Or do you have special PathComponents?

那这个呢。或者你有特殊的 PathComponents 吗?

public void jTree1ValueChanged( TreeSelectionEvent tse ) {
     String node = tse.getNewLeadSelectionPath().getLastPathComponent().toString();
    if( node.equals("audio") ) {
        // play audio
    } else if( node.equals("video") ) {
       // play video
    }
}

回答by Mohayemin

You cannot add an event listener to tree-node because the class representing the tree-node is not a Component.

您不能向树节点添加事件侦听器,因为表示树节点的类不是组件。