File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes how to add a new node to already existing node but i dont know the parent node Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to add a new node to already existing node but i dont know the parent node" Watch "how to add a new node to already existing node but i dont know the parent node" New topic
Author

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

suganya vijaykumar
Greenhorn

Joined: Jun 22, 2008
Posts: 12
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
suganya vijaykumar
Greenhorn

Joined: Jun 22, 2008
Posts: 12
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Sep 20, 2005
Posts: 862
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.


bitguru blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to add a new node to already existing node but i dont know the parent node