Article View: borland.public.jbuilder.java.swing
Article #12512Re: Adding nodes to a JTree
From: Jason Burton
Date: Wed, 16 Dec 1998 00:00
Date: Wed, 16 Dec 1998 00:00
32 lines
2000 bytes
2000 bytes
Leo Cohen wrote in message <3677EFE9.F585563F@cs.virginia.edu>... >Using JTree, I can succesfully initialize a tree using the default tree >model and DefaultMutableTreeNode. After the initialized tree is on the >screen, how do I add a new node to the tree? > Sorry for continuing our thread in this group, but it seemed that the java.language group was emptied out somehow. Hopefully it's just my setup. Anyway, since I use such a heavily customized subclass of JTree, I left out the part about refreshing the tree after adding nodes. In your case, you were using a DefaultTreeModel, so you can do the following typecast: DefaultTreeModel model = (DefaultTreeModel)tree.getModel(); You can use this class to refresh items after changing nodes. There are several methods, depending on what you change in the nodes. reload(TreeNode) will work to completely refresh your tree. If you use the JTree (or any Swing component for that matter), you might consider making your own custom subclass to handle these updates automatically that is more programmer friendly. In the case of my JTree subclass, I also subclass TreeNode so that changes in a node (adding children) will automatically refresh the tree appropriately, unless I have a "hold painting" flag turned on. I felt that generally, I wanted the tree to refresh as I added the nodes, but sometimes I wanted to add several hundred nodes without the refresh in between each node. Hope this helps. Jason Burton
Technical Details
Message-ID:
<759fu4$rd419@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3677EFE9.F585563F@cs.virginia.edu>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,8275605b90fd1c2b X-Google-Attributes: gid111d45,public From: "Jason Burton" <jasonb@tmscorp.com> Subject: Re: Adding nodes to a JTree Date: 1998/12/16 Message-ID: <759fu4$rd419@forums.borland.com> X-Deja-AN: 423051804 References: <3677EFE9.F585563F@cs.virginia.edu> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0 Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.swing