Solved

Cloning nodes in JTree

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

697 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