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



Relevant Pages

  • Re: Cantor Confusion
    ... the nodes of the tree are connected by an untearable network. ... but by construction of the tree. ... Are you just discovering that? ... Therefore it is of no interest at all to speculate ...
    (sci.math)
  • Re: Cantor Confusion
    ... the nodes of the tree are connected by an untearable network. ... but by construction of the tree. ... Are you just discovering that? ...
    (sci.math)
  • JTree, updating model and DefaultMutableTreeNode
    ... I am discovering the Swing's tree API. ... steps of a long-running process). ... public class TreeAddNodes ...
    (comp.lang.java.gui)