Dieter Pollier
Dieter Pollier

Reputation: 839

Java JTree: only add child if there are no other childs

I have a JTree with a root and several nodes. When the user adds a node to another node with no children, the child is added. But when there are already nodes in the selected node, the node can't be added.

This is my code:

DefaultMutableTreeNode selectedNode =  DefaultMutableTreeNode)treeExpertises.getSelectionPath().getLastPathComponent();
selectedNode.insert(new DefaultMutableTreeNode(newDomain), selectedNode.getChildCount());

I also tried this with the same result:

DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)treeExpertises.getSelectionPath().getLastPathComponent();
selectedNode.add(new DefaultMutableTreeNode(newDomain));

Upvotes: 0

Views: 45

Answers (1)

Dieter Pollier
Dieter Pollier

Reputation: 839

I already found the answer:

DefaultTreeModel model = (DefaultTreeModel)treeExpertises.getModel();
model.nodeStructureChanged(selectedNode);

Upvotes: 0

Related Questions