• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to add a new node to already existing node but i dont know the parent node

 
suganya vijaykumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem with adding a new node to already existing node.... But i don't know the name of the particular parent node.... Please help me to solve this...............
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me get this straight.

You want to add a TreeNode to another TreeNode, but you don't know which TreeNode?
What are the criteria for the TreeNode?
 
suganya vijaykumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
Let me get this straight.

You want to add a TreeNode to another TreeNode, but you don't know which TreeNode?
What are the criteria for the TreeNode?


yes.. exactly...
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then please answer my last question: What are the criteria for the TreeNode?

What makes the TreeNode you want to add to so different from all other TreeNodes?
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Cole
Author
Ranch Hand
Posts: 903
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic