Article View: borland.public.jbuilder.java.swing
Article #10750Re: VERY basic JTree question...
From: Holger Stratmann
Date: Tue, 17 Aug 1999 00:00
Date: Tue, 17 Aug 1999 00:00
70 lines
2939 bytes
2939 bytes
Thanks a lot for your very fast answer :-)
It was not completely correct, but as I said: I just needed something to get
me started. Basically, it was the "DefaultMutableTreeNode" that I needed - I
would have never expected to be expected to use such a class *bg*
In your code, you
a) use "parent1" / "parent2" again and overwrite the initial value
b) never add the children to the tree.
The working code looks like this:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("parent1");
root.add(parent1);
parent1.add(new DefaultMutableTreeNode("child11"));
parent1.add(new DefaultMutableTreeNode("child12"));
DefaultMutableTreeNode parent2 = new DefaultMutableTreeNode("parent2");
root.add(parent2);
parent2.add(new DefaultMutableTreeNode("child21"));
parent2.add(new DefaultMutableTreeNode("child22"));
jTree1 = new JTree(root);
Thanks anyway - your hint was all I needed (for the moment) :-))
Jonathan wrote:
> JTree tree;
>
> in the initialisation method do:
>
> DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
> DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("parent1");
> parent1 = new DefaultMutableTreeNode("child11");
> parent1= new DefaultMutableTreeNode("child12");
> DefaultMutableTreeNode parent2 = new DefaultMutableTreeNode("parent2");
> parent2 = new DefaultMutableTreeNode("child21");
> parent2 = new DefaultMutableTreeNode("child22");
>
> root.add(parent1);
> root.add(parent2);
>
> tree = new JTree(root);
>
> This will work, but it is better to write a class which handles such data
> and creation of nodes, but this will definately get you started
>
> if you need any more help, just ask
>
> Holger Stratmann wrote in message <37B97436.60A20FCB@cassiopeia.de>...
> >This is just for starters: How do I insert a couple of nodes into a
> >JTree?
> >
> >e.g. what's the code for
> >* Root
> > |- Parent 1
> > | |- Child 11
> > | '- Child 12
> > '- Parent 2
> > '- Child 21
> >
> >I just keep getting lost in all those interfaces and abstract classes.
> >Once I can create one parent and one child, I can probably just go on...
> >
> >Thanks a lot! :-)
> >
> >
Technical Details
Message-ID:
<37B98255.B4B8E4DC@cassiopeia.de>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<37B97436.60A20FCB@cassiopeia.de> <7pbslu$sci2@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,a250cac0519badf0 X-Google-Attributes: gid111d45,public From: Holger Stratmann <hs@cassiopeia.de> Subject: Re: VERY basic JTree question... Date: 1999/08/17 Message-ID: <37B98255.B4B8E4DC@cassiopeia.de> X-Deja-AN: 513828607 Content-Transfer-Encoding: 7bit References: <37B97436.60A20FCB@cassiopeia.de> <7pbslu$sci2@forums.borland.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Mime-Version: 1.0 Reply-To: Holger@cheerful.com Newsgroups: borland.public.jbuilder.java.swing