I'm having an issue updating my JTree when I add a node to the tree.
When I add to the JTree directly, I do the following (this method is
in my class that extends JTree)
public void addNode(DataModel parent, DataModel newChild) {
// create new node and expand the path to show the node
((DataModel) parent).insert(newChild, parent.getChildCount());
// if a successful addition
if (newChild.getParent() != null &&
newChild.getParent().equals(parent)) {
treeModel.addTreeModelListener(newChild);
// update tree and scenario values
newChild.updateScenarioModel();
// make the tree an observer of the DataModel (and the DataModel's
// children)
this.addObservers(newChild);
((DataModel) newChild.getParent()).sort();
this.updateUI();
}
DataModel implements MutableTreeNode and the root node is passed in to
the JTree on creation.
So, I'm basically adding everything directly to the JTree. Honestly,
now that I think about it, that doesn't entirely seem right as the
JTree should be *just* a view. So, I have been trying, instead, to
add to the node. However, the JTree doesn't update or refresh when I
add the node.
I've been reading online, and there is a lot of discussion about
TreeModel's, but I am not sure if I entirely understand how this plays
into the TreeNodes or how they work together.
Any further explanation would be welcomed. Thanks.
Sabine Dinis Blochberger - 26 Jul 2007 09:23 GMT
> I'm having an issue updating my JTree when I add a node to the tree.
When I create a new tree structure, I call
model.reload();
After changing a child node (setting the user object), I call
model.nodeChanged(child);
If you use model.insertNodeInto(), you don't need to tell the tree to
update, the method does it already.
Hope this will help.

Signature
Sabine Dinis Blochberger
Op3racional
www.op3racional.eu
Jason Cavett - 01 Aug 2007 19:36 GMT
On Jul 26, 4:23 am, Sabine Dinis Blochberger <no.s...@here.invalid>
wrote:
> > I'm having an issue updating my JTree when I add a node to the tree.
>
[quoted text clipped - 12 lines]
>
> Op3racionalwww.op3racional.eu
That will work if I have access to the JTree's model. But, currently,
I only have access to the nodes that makeup the JTree (DataModel).
The DataModel != TreeModel.
Because DataModel implements MutableTreeNode, it does have an "insert"
method where I can add a child to the DataModel. It seems to me that
there should be some way for the DataModel (as a node) to let its Tree
know that it had a child added to it.