Solved

Cloning nodes in JTree

Posted on 2001-06-13
4
1,408 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

911 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

22 Experts available now in Live!

Get 1:1 Help Now