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