JTree, updating model and DefaultMutableTreeNode - Java

This is a discussion on JTree, updating model and DefaultMutableTreeNode - Java ; 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 ...

+ Reply to Thread
Results 1 to 2 of 2

JTree, updating model and DefaultMutableTreeNode

  1. Default JTree, updating model and DefaultMutableTreeNode

    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


  2. Default Re: JTree, updating model and DefaultMutableTreeNode

    Florent Georges wrote:
    > 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"));


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


    >
    > // 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!


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

+ Reply to Thread

Similar Threads

  1. JTree Selection lost when JTree looses focus
    By Application Development in forum Java
    Replies: 3
    Last Post: 11-12-2007, 10:55 AM
  2. Updating JTree When Adding Nodes
    By Application Development in forum Java
    Replies: 2
    Last Post: 08-01-2007, 01:37 PM
  3. JTree not updating after deleting a node in DnD
    By Application Development in forum Java
    Replies: 4
    Last Post: 07-24-2007, 02:42 AM
  4. Replies: 4
    Last Post: 05-19-2007, 05:21 PM