This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
adding a new node to already existing node.... But i don't know the name of the particular parent node If your tree uses DefaultMutableTreeNode then you can use methods of this class to add/insert nodes to/into the node. To identify the parent node you can get a reference to it: 1 — from a TreeSelectionListener which you add to the trees TreeSelectionModel. With this you can know when the selection state has changed and then get a reference to the selected node(s) either by using JTree methods, eg, getSelectionPath, or by using TreeSelectionEvent methods inside the selectionListener.
2 — or you can traverse the tree and search for the userObject. If the userObject is or returns a String you can look for it in each node of the tree.
With this reference you can append the child to the parent node with the DefaultMutableTreeNode method add or you can insert the node into the nodes children with the insert method.
Originally posted by Craig Wood: adding a new node to already existing node.... But i don't know the name of the particular parent node If your tree uses DefaultMutableTreeNode then you can use methods of this class to add/insert nodes to/into the node.
If you're using DefaultTreeModel then you should use its methods to add/insert nodes, not the DefaultMutableTreeNode methods.
So yourModel.insertNodeInto(newChild, yourNode, 3) instead of yourNode.insert(newchild, 3).
This way the proper events will be fired to allow newChild to actually become visible in the tree. Otherwise newChild will have been added from the model's point of view, but the JTree won't be aware of it unless you fire your own events.