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/