Emalka
Emalka

Reputation: 379

Add new node to existing node in JavaTree using Java

I have a already created java tree. Now I want to add new node to existing node in this Java tree. This existing node can be any where in the tree.

My tree nodes are included in a HashMap and each node have key(String) and value(Double). I know its key and value. So, how to add this node to correct existing node?

  DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new NodeInfor(check, 0.45));  

            nodeReg.put(check, newNode);
            ?????.add(newNode);

In above code for ????? what I need to used? Existing node is I already created.But In this point I select it randomly and I know only its key and value.

If I want to create a separate another JavaTree after adding this new node,

                tree = new JTree(root); ------------------????
                add(tree);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setTitle("Sub JTrees");
                this.pack();
                this.setVisible(true);

in above code, tree = new JTree(root); what I need to add for root?

UPDATE: More coding part:

        //create root
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(new NodeInfor(keys.get(0), values.get(0)));
        // create the child nodes
        DefaultMutableTreeNode aNode = new DefaultMutableTreeNode(new NodeInfor(keys.get(1), values.get(1)));
        DefaultMutableTreeNode bNode = new DefaultMutableTreeNode(new NodeInfor(keys.get(2), values.get(2)));

           nodeReg.put(keys.get(0), root);
           nodeReg.put(keys.get(1), aNode);
           nodeReg.put(keys.get(2), bNode);

           root.add(aNode);
           root.add(bNode);
           -----------
           -----------
             ---------
           ---------
            tree = new JTree(root);
            add(tree);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setTitle("First JTree");
            this.pack();
            this.setVisible(true);
             ---------
             ----------
             ----------
           DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new NodeInfor(check, 0.45));  

            nodeReg.put(check, newNode);
            ?????.add(newNode);-----------------?????????

            tree = new JTree(???????????);------------??????
            add(tree);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setTitle("Sub JTree");
            this.pack();
            this.setVisible(true);
            }

I want to add new node to keys.get(1), values.get(1) . So how to add and display new tree again?

Upvotes: 0

Views: 800

Answers (1)

js_248
js_248

Reputation: 2122

I hope nodeReg is map, So you can get anode by callling get method and passing keys.get(1) as key.

DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new NodeInfor(check, 0.45));  
nodeReg.put(check, newNode);
DefaultMutableTreeNode node = nodeReg.get(keys.get(1));
node.add(newNode); //add new node to anode
tree = new JTree(root);

Now this tree will be having four nodes

root,aNode,bNode,newNode

Upvotes: 1

Related Questions