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 Copy subtree in jtree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Copy subtree in jtree" Watch "Copy subtree in jtree" New topic
Author

Copy subtree in jtree

Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Imagine, if you will, a method that makes a copy of a subtree from an existing swing.jTree filled with string objects. Having this subtree copied and saved in a DefaultMutableTreeNode variable, would allow you to paste said subtree anywhere you would like in the tree. Below is my last fruitless attempt to do the infuriating. Can anyone help me to understand why the path information is being lost? All this does is collect the children, subchildren, subsubchildren etc… and paste them all as one level. It loses all directory structure. Any help leading me to a solution that you can provide would be greatly appreciated.


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I would have expected to see a recursive method to clone a tree. And I would have expected the method to take a parameter containing the root of the tree to be cloned. But I don't see either of those things. Perhaps you could explain in detail how the method decides which tree is supposed to be cloned and how you access all the descendants of the root of that tree, because I don't see any code like that in your post.
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Oh, ok. The jTree1.getSelectionPath().getLastPathComponent() is grabbing and cloning a node (which is selected when this method is called) for use as a root for the new subtree.
I then use the current selected location (saved in "theSource") to guide me down the existing path of the original jtree. I use a for loop to allow the correct number of loops for the number of children. inside the loop I use the *.getNextNode(); which uses the inorder traversal of a tree, starting at "theSource" which is the subtree I wish to copy. I have the ‘if’ statement to check to make sure that after the *.getNextNode is called that it is not null. If it isn't, I clone the child and add it to the subtree root named "theClone".

Now that that is nice and explained, let’s just say that I am aware that this code is garbage and should be set on fire at my first opportunity. I get that I am cloning nodes and not saving path information in this method. I guess, I assumed that there was a pre-existing method that would do an inorder traversal of the tree from a selected location inorder to allow the copying of subtrees. I am beginning to believe that no such thing exists and I will be forced to write my own recursive traversal. It seems a shame as I see that it has been coded in many locations, like the enumerations for example. I feel like I am re-inventing the wheel.

Now that my book of moaning and groaning has been completed, perhaps I could ask for some idea on how to properly implement the subtree copy.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

So you are looking at all of the descendants of the original root, in some order, and adding them all as immediate children to the new root.

Here's pseudo-code for what a tree-cloning method should look like:
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Oddly enough, I figured it out on my own. Looking at your pseudocode makes me think I may have made it more complecated than it should have been. Perhaps you can look at this code and tell me if you think it is acceptablly written (i.e. sound reasoning).
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Paul Clapham:
You have been more help in a few hours than countless people have been in days. Thank you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Just one improvement: you're iterating over the children twice. You can combine those two loops:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
Boss! AKA nice! I love to see my code optimized. I sometimes forget in the excitement of getting it to work, that efficiency is the next step before you are really done. While two loops that are non-nested moved to one does not change Big-O it does change your little 'o'. Sometimes that is the difference between selling your code and not selling your code. Thank you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

jake benn wrote:I love to see my code optimized. I sometimes forget in the excitement of getting it to work, that efficiency is the next step before you are really done.

Well, at least you got the main principle of programming right: get it to work first, worry about performance afterwards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copy subtree in jtree