如何实现监听器?
我有一些动态树。现在我需要实现一些功能,每次只需单击节点即可实现这些功能。 (我的意思是只需单击节点一次,“使其变为蓝色”)
**EDIT2:** 我使用 beanTreeView 和包 openide
如何实现此操作的侦听器?
编辑 - 添加伪代码
public class MyNode extends AbstractNode{ //openide package
private String name;
public MyNode(String nameOfNode){
super (new Children.LEAF);
name = nameOfNode;
}
....
....
}
public class IWantNameOfSelectedNode extends JPanel{
private JLabel jLnameOfNode;
public IWantNameOfSelectedNode(){
jLnameOfNode.setText("wiating for node selection");
}
现在,我需要将所选节点的名称放入 jLabel,并在每次节点选择更改时更改它。
I have some dynamic tree. And now I need to implement some functionality, that happend every time, when simply click on node. (I mean only one click on node, which "makes it blue")
**EDIT2: ** I use beanTreeView and package openide
How to implement listener of this action?
EDIT - added pseudocode
public class MyNode extends AbstractNode{ //openide package
private String name;
public MyNode(String nameOfNode){
super (new Children.LEAF);
name = nameOfNode;
}
....
....
}
public class IWantNameOfSelectedNode extends JPanel{
private JLabel jLnameOfNode;
public IWantNameOfSelectedNode(){
jLnameOfNode.setText("wiating for node selection");
}
Now, I need put name of selected node to jLabel, and change it every time when selection of node changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您使用的是 Swing
JTree
类,您应该定义一个TreeSelectionListener
并将其添加到底层TreeModel
中。如果您希望使用ActionListener
,则需要编写一些适配器代码来将TreeSelectionEvent
转换为ActionEvent
s(尽管这实际上毫无意义)。示例
Assuming you're using the Swing
JTree
class you should define aTreeSelectionListener
and add it to the underlyingTreeModel
. If you wish to useActionListener
instead you'll need to write some adapter code to translateTreeSelectionEvent
s intoActionEvent
s (although this would actually be fairly pointless).Example
我假设它是一棵秋千树。您可以通过使用 CustomRenderer 组件或使用 TreeSelectionListener 接口来实现此目的。
此链接有一个关于高级示例的教程如何更改图标、背景等。您需要的是一个比这简单得多的版本。
您感兴趣的代码是
I am assuming it is a Swing Tree. You could achieve this by using a CustomRenderer component or using a TreeSelectionListener interface.
This link has a tutorial for an advanced Example on how to alter the icons, backgrounds etc. What you need is a much simpler version than that.
The code you will be interested in is