Article View: borland.public.jbuilder.java.language
Article #17055Re: Adding nodes to JTree
From: Leo Cohen
Date: Wed, 16 Dec 1998 00:00
Date: Wed, 16 Dec 1998 00:00
65 lines
2655 bytes
2655 bytes
I've tried your suggestion in a test case but I'm not getting a new node
on the screen. Here's the structure:
public TreeSelExample()
{
// ...
rootNode = new DefaultMutableTreeNode("Categories");
// ... two children are added under the parent
// here's an example for one of them:
childNode = new DefaultMutableTreeNode("Metals");
rootNode.add(childNode);
// ...
// Then the display is setup and put on the screen,
// ending the initiation of the tree. So far, so good.
The following method is in this class;
public void valueChanged(TreeSelectionEvent e) {
chileNode = new DefaultMutableTreeNode("Fluids");
rootNode.add(childNode);
// ...
}
This method is called on selection of a node in the tree, and here is
where I want to add a new node, as shown. The code compiles, and when
run only shows the first two children. I don't seem to be able to add
the third child, "Fluids", to the tree.
The setVisible(true) has occurred after the initiation and before the
addition. Do I have to refresh the display to get the new node to show?
If so, what refreshes a JTree display?
--------
Jason Burton wrote:
>
> Leo Cohen wrote in message <3677EF96.BDBE2B67@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?
>
> You use the methods of the root node (and any child nodes), rather than
> using methods of the JTree itself. Example:
>
> DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root Node");
> JTree myTree = new JTree(rootNode);
> DefaultMutableTreeNode childNode = new DefaultMutableTreeNode("Child Node");
> rootNode.add(childNode);
>
> See the help on DefaultMutableTreeNode for more methods to manipulate child
> nodes.
>
> Jason Burton
--
Leo Cohen
116-1 Howard Drive
Charlottesville, VA 22903
804-243-8922 (UVA)
804-244-4648 (home)
804-244-7922 (fax)
lcohen@cs.virginia.edu
Technical Details
Message-ID:
<3678172B.E65682F5@cs.virginia.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3677EF96.BDBE2B67@cs.virginia.edu> <758s82$qss15@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,c4ccd4f3d2110752 X-Google-Attributes: gidf74b3,public From: Leo Cohen <lcohen@cs.virginia.edu> Subject: Re: Adding nodes to JTree Date: 1998/12/16 Message-ID: <3678172B.E65682F5@cs.virginia.edu> X-Deja-AN: 422991605 Content-Transfer-Encoding: 7bit References: <3677EF96.BDBE2B67@cs.virginia.edu> <758s82$qss15@forums.borland.com> Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language