Updating JTree When Adding Nodes

J

Jason Cavett

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

Sabine Dinis Blochberger

Jason said:
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.
 
J

Jason Cavett

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top