简体   繁体   中英

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.

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. For example, if you're using 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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