How to add and remove nodes in Jtree?
You have a couple of choices:
Update the model directly:
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
model.insertNodeInto(new DefaultMutableTreeNode("another_child"), root, root.getChildCount());
Update the tree nodes and then notify the model:
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
root.add(new DefaultMutableTreeNode("another_child"));
model.reload(root);
The same applies for removing nodes.
The DefaultTreeModel has a removeNodeFromParent(...) which will update the model directly.
Or you can use the remove(...) method of the DefaultMutableTreeNode class. In which case you would need to do the reload().
| Is This Answer Correct ? | 0 Yes | 0 No |
what is the difference between HashMap and Hashtable
17 Answers Value Labs, Virtusa,
Describe the syntax of multiple inheritance? When do we use such an inheritance?
Is the empty set a singleton?
what is Thread priority?
4 Answers Tech Mahindra, Wipro,
What is the difference between Grid and Gridbaglayout?
what are the difference between Java and .Net?
Why are variables important in research?
why we need this (1.object,2.class,3.data hiding,4.encapsulation,5.abstraction,6. polymorphism,7.inheritance)
What is tree node in java?
What does || || mean in math?
what are the different non-access specifiers in java?
What are memory tables?