Solved

Cloning nodes in JTree

Posted on 2001-06-13
4
1,477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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:

695 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