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 are the advantages of exception handling?
What do you understand by the term singleton?
What are java threads?
Is empty set an element of empty set?
What do you mean by exception handling in Java?
Which is fastest collection in java?
How does marker interface provides functionality to the implemented class ? or How dose maker interface gets the functionalities as serialization or cloning.
What is the private method modifier?
What is meant by main method?
How can we avoid including a header more than once?
How many types of thread in java? give the name
How do I convert a string to an int in java?