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

454 views Asked by At

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.

1

There are 1 answers

4
Roberto Attias On

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);
 }