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 / October 2007

Tip: Looking for answers? Try searching our database.

JTree, updating model and DefaultMutableTreeNode

Thread view: 
Florent Georges - 17 Oct 2007 14:30 GMT
Hi

 I am discovering the Swing's tree API.  I am building a
tree one node after the other (the nodes reflect different
steps of a long-running process).

 The problem is that the nodes added after the construction
of the tree are not shown.  I guess I have to tell the tree
in some way that the tree model has changed, but I don't
know how.

 Below is a full simple sample that reproduce the same
issue I have, I think:

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.WindowConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class TreeAddNodes
{
   public static void main(String[] args)
   {
       // first part of the model
       DefaultMutableTreeNode root =
           new DefaultMutableTreeNode("node 1");
       root.add(new DefaultMutableTreeNode("node 2"));
       root.add(new DefaultMutableTreeNode("node 3"));

       // the model and the tree
       JTree tree = new JTree(new DefaultTreeModel(root));

       // second part of the model, NOT SHOWN!
       root.add(new DefaultMutableTreeNode("node 4"));
       root.add(new DefaultMutableTreeNode("node 5"));

       // GUI boilerplate
       JFrame frame = new JFrame();
       frame.setDefaultCloseOperation(
                     WindowConstants.EXIT_ON_CLOSE);
       frame.setSize(200, 200);
       frame.add(tree);
       frame.show();
   }
}

 Any comment greatly appraciated!

 Regards,

--drkm
RedGrittyBrick - 17 Oct 2007 15:45 GMT
>   Hi
>
[quoted text clipped - 32 lines]
>         root.add(new DefaultMutableTreeNode("node 4"));
>         root.add(new DefaultMutableTreeNode("node 5"));

Maybe
    tree.getModel().fireTreeNodesInserted(...);

>         // GUI boilerplate
>         JFrame frame = new JFrame();
[quoted text clipped - 7 lines]
>
>   Any comment greatly appraciated!

http://java.sun.com/products/jfc/tsc/articles/jtree/


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.