Solved

Cloning nodes in JTree

Posted on 2001-06-13
4
1,397 Views
Last Modified: 2011-10-03
Hi I have a basic tree.

Now I want to create Tree A and Tree B which will
have the nodes of basic tree and some additional
nodes.

I get the nodes of the basic tree by
 DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode)basicTree.                          getModel().getRoot();

Enumeration enum = rootNode.children();
childCount = rootNode.getChildCount();

The problem is once I add these nodes to Tree A
I cannot add these nodes to Tree B.

infact after adding the nodes to Tree A
if I do : childCount = rootNode.getChildCount();
I get childCount = 0;

I dont know how the basic tree gets affected
I think I have to do deep cloning and then
work on the  cloned node.

Any suggestions?

If I can see come code snippet of deep cloning
a node . it will be helpful

Structure of tree example:

fab ---- root node
 name ----node
  address --node
    city  --node

each of the children name, address, city
have multiple nodes.
I want to copy all the children of
the root node to multiple trees.
 











 

0
Comment
Question by:sanjay_thakur
  • 3
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6189025
Well without seeing the code that's adding nodes it's a bit hard to help.
Sounds though, like what you need to do is create new instance of the node from tree A, to add to the tree B.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6189040
Here is deepClone sample:

MutableTreeNode deepClone(MutableTreeNode root){
    MutableTreeNode newRoot = (MutableTreeNode)root.clone();
    // Take care of user object cloning, if necessary.
    for(Enumeration childEnum = root.children(); childEnum.hasMoreElements();){
           newRoot.add(deepClone((MutableTreeNode)childEnum.nextElement()));
    }
}

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
0
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 200 total points
ID: 6189042
Oops, sorry. return newRoot before last brace.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6192645
sanjay_thakur,

Most typical grades are A and B here. People typically explain source of dissatisfaction when assign "C" grade to answer.  If you have more questions anyone will try to make explanation more clear.

Thanks,
Igor Bazarny
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now