简体   繁体   English

TreeView-Jtree(NetBeans)如何向节点添加描述

[英]TreeView - Jtree (NetBeans) How to add a description to a node

Im making a simple treeview on NetBeans and id like to know how can i add a description to a determined selected node, through a button that have a function that will associate to a lable. 我在NetBeans和id上制作了一个简单的树形视图,想知道如何通过按钮将其添加到确定的选定节点上,该按钮具有与标签相关的功能。

Click to see Treeview Image here 单击此处查看树视图图像

the link shows the image of what i want to do, by clicking ">>" it will add a description to that lable and associate to that selected node. 该链接显示了我要执行的操作的图像,通过单击“ >>”,它将为该标签添加描述并与所选节点关联。

this is the code for the ">>" button. 这是“ >>”按钮的代码。

private void add2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
   lTree2.setText(tf2.getText());
}

obviously this isnt what i want, i just put here show what i want. 显然,这不是我想要的,我只是在这里展示了我想要的。

You want to create your own class for tree nodes, as a subclass of whatever you're using now, adding a description field and corresponding accessors in the subclass. 您想为树节点创建自己的类,作为您现在正在使用的任何子类,并在子类中添加description字段和相应的访问器。 For example, if you're using DefaultMutableTreeNode : 例如,如果您使用的是DefaultMutableTreeNode

class MyNode extends DefaultMutableTreeNode {
    private String description;
    ...
    public void setDescription(String descr) {
        description = descr;
    }

    public String getDescription() {
        return description;
    }
}

Once you've done that, in your actionPerformed() for the button you want to get the selected tree node, get the description out of it, and set the text in the label: 完成此操作后,在按钮的actionPerformed() ,要获取选定的树节点,获取其描述,然后在标签中设置文本:

private void add2ActionPerformed(java.awt.event.ActionEvent evt)
{                                     
    MyNode node = (MyNode)tree.getLastSelectedPathComponent();
    String descr = node.getDescription();
    lTree2.setztext(descr);
 }        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM