Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2005

Tip: Looking for answers? Try searching our database.

How can I add another MutableTreeNode to a tree?

Thread view: 
Gil - 27 Jan 2005 10:14 GMT
Does anyone know the method to add another MutableTree Node to a tree
after a DefaultMutableTreeNode has been added?  Or how to add another
node to the tree?

I tried to add a 2nd DefaultMutableTreeNode to a tree with add() but
got an error.  My code follows :

     // create node 1 of tree2
     DefaultMutableTreeNode top2 = new
DefaultMutableTreeNode("Node1");
     
     // create subtree of "A"
     DefaultMutableTreeNode aa = new DefaultMutableTreeNode("Sub node
1");
     top2.add(aa);
     DefaultMutableTreeNode aa1 = new DefaultMutableTreeNode("A1");
     aa.add(aa1);
     DefaultMutableTreeNode aa2 = new DefaultMutableTreeNode("A2");
     aa.add(aa2);

     // create subtree of "B"
     DefaultMutableTreeNode ab = new DefaultMutableTreeNode("Sub node
2");
     top2.add(ab);
     DefaultMutableTreeNode ab1 = new DefaultMutableTreeNode("B1");
     ab.add(ab1);
     DefaultMutableTreeNode ab2 = new DefaultMutableTreeNode("B2");
     ab.add(ab2);
     DefaultMutableTreeNode ab3 = new DefaultMutableTreeNode("B3");
     ab.add(ab3);

     // create node 2 of tree2
     DefaultMutableTreeNode top2a = new DefaultMutableTreeNode("Node
2");
     
     // create subtree of "A"
     DefaultMutableTreeNode a2a = new DefaultMutableTreeNode("Sub
node 3");
     top2a.add(a2a);
     DefaultMutableTreeNode a2a1 = new DefaultMutableTreeNode("A1");
     a2a.add(a2a1);
     DefaultMutableTreeNode a2a2 = new DefaultMutableTreeNode("A2");
     a2a.add(a2a2);

     // create subtree of "B"
     DefaultMutableTreeNode a2b = new DefaultMutableTreeNode("Sub
node 4");
     top2a.add(a2b);
     DefaultMutableTreeNode a2b1 = new DefaultMutableTreeNode("B1");
     a2b.add(a2b1);
     DefaultMutableTreeNode a2b2 = new DefaultMutableTreeNode("B2");
     a2b.add(a2b2);
     DefaultMutableTreeNode a2b3 = new DefaultMutableTreeNode("B3");
     a2b.add(a2b3);

     JTree tree2 = new JTree(top2);
     tree2.add(top2a);   // this line gave me the error
                         // cannot resolve symbol add()
Thomas Weidenfeller - 27 Jan 2005 10:32 GMT
> Does anyone know the method to add another MutableTree Node to a tree
> after a DefaultMutableTreeNode has been added?  Or how to add another
> node to the tree?
[...]
>       JTree tree2 = new JTree(top2);
>       tree2.add(top2a);   // this line gave me the error
>                           // cannot resolve symbol add()

You try to add the node to the JTree, which simply doesn't have an add()
method. Add the node to the model. Since you don't have an explicit
model (not a good idea), you have to get the default one from the JTree.
If the model is implemented correctly (DefaultTreeModel is), then the
model informs the JTree about the changes (see also Q6.2.1 in the FAQ).

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.