Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recursively "copying" JTree nodes...

276 views
Skip to first unread message
Message has been deleted

Ron Blanford

unread,
Aug 3, 1999, 3:00:00 AM8/3/99
to

If you implement lazy loading in your custom tree node, then you don't
have to enumerate the children when making the copy. Just copy the root
node and mark the copy "not loaded". When you later paste it in another
place and navigate to it, the load mechanism will note that it's
unexpanded and create the children for you at that time.

The SampleTree example in Swing shows one way to implement lazy loading.

-- Ron

Theron Kousek wrote:
>
> I need to "recursively" create a copy(need to create a brand new physical
> copy) of a tree node and all of it's children (which includes grand children
> and great grandchildren, etc..)...
>
> Can someone tell me what methods I should be looking at in order to
> accomplish this?
>
> I've been looking at the Enumeration process to enumerate thru all of the
> nodes and descendents but am a little confused.
>
> Regards,
> Theron K

Robert F Walker

unread,
Aug 3, 1999, 3:00:00 AM8/3/99
to
public DefaultMutableTreeNode cloneNode(DefaultMutableTreeNode node)
{
DefaultMutableTreeNode newNode =
new
DefaultMutableTreeNode(node.getUserObject());

for(int iChildren=node.getChildCount(), i=0;
i<iChildren; i++)
{
newNode.add( (MutableTreeNode)cloneNode(
(DefaultMutableTreeNode)node.getChildAt(i) ) );
}

return newNode;
}

If you pass it a root node, you'll get back a complete
copy of it and its children. Christian (who posts here ofter)
helped me with this a while back

robert
Matt. 6:33

Christian Kaufhold

unread,
Aug 3, 1999, 3:00:00 AM8/3/99
to
Theron Kousek <the...@sprynet.com> wrote:
> I need to "recursively" create a copy(need to create a brand new physical
> copy) of a tree node and all of it's children (which includes grand children
> and great grandchildren, etc..)...
[...]

public DefaultMutableTreeNode clone(DefaultMutableTreeNode node)
{
Whatever w = node.userObject(); // skip these lines if you don't
Whatever wClone = w.clone(); // want to clone the user objects

DefaultMutableTreeNode clone = new DefaultMutableTreeNode(wClone);

for (int len = node.getChildCount(), i = 0; i < len; i++)
clone.add(clone(node.getChildAt(i))); // recursive call

return clone;
}

(easily adapted if you do not use DefaultMutableTreeNodes).

Followup-to set.


Christian

0 new messages