I'm trying to update a JTree after it's already been created, so after
a few operations, I add more nodes to my root node and would like to
update it in my scrollpane. My problem is, when I add nodes to it, I
don't know how to make it visually update.
I have code like this that get's dynamically appended to throughout my
program.
---
DefaultMutableTreeNode books = new DefaultMutableTreeNode("books");
for (int i = 0; i < books.size(); i++) {
books.add(new DefaultMutableTreeNode("Book: " + i));
}
root.add(books);
// now how to make the JTree visually update in the scrollpane
---
Any help much appreciated.
Pete Barrett - 07 Sep 2005 18:43 GMT
>I'm trying to update a JTree after it's already been created, so after
>a few operations, I add more nodes to my root node and would like to
[quoted text clipped - 14 lines]
>root.add(books);
>// now how to make the JTree visually update in the scrollpane
revalidate() usually works for me. Also available are 2 repaintS, 2
paintImmedatelyS, and probably others as well!
Pete Barrett
Roland - 07 Sep 2005 19:25 GMT
> I'm trying to update a JTree after it's already been created, so after
> a few operations, I add more nodes to my root node and would like to
[quoted text clipped - 18 lines]
>
> Any help much appreciated.
Use the tree's model; by default, JTree's TreeModel is an instance of
DefaultTreeModel.
You can use DefaultTreeModel.insertNodeInto for each node separately
(order is important, so you'll have to move the code to add 'books' to
'root' to just *before* the for-loop), or use
DefaultTreeModel.nodeStructureChanged or
DefaultTreeModel.nodesWereInserted after adding a batch of nodes.
<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/tree/DefaultTreeModel.html>
<http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#dynamic>

Signature
Regards,
Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
Babu Kalakrishnan - 07 Sep 2005 19:38 GMT
> I'm trying to update a JTree after it's already been created, so after
> a few operations, I add more nodes to my root node and would like to
> update it in my scrollpane. My problem is, when I add nodes to it, I
> don't know how to make it visually update.
The preferred way to mutate the tree structure is to use methods
provided in the model to do so (Look at the API docs of DefaultTreeModel
: insertNodeInto method). If for some reason you cannot do this and need
to add nodes directly, you would need to inform the model of these
changes by calling its notification methods (nodesWereInserted,and other
similar named ones).
BK
Roedy Green - 10 Sep 2005 12:16 GMT
>I'm trying to update a JTree after it's already been created, so after
>a few operations, I add more nodes to my root node and would like to
>update it in my scrollpane. My problem is, when I add nodes to it, I
>don't know how to make it visually update.
The basic trick is code like this:
ConstellationTreeModel.model.fireTreeStructureChanged(
ConstellationTreeModel.root , new TreePath ( new Object[] {
ConstellationTreeModel.root} ));
You update your model, then fire the appropriate change event. Swing
will then arrange to repaint the affected nodes.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.