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 starvation?
Why do we need hashmap in java?
What is integer valueof?
What is primitive array?
What is the size of int?
What is null in java?
How will you call an Applet using Java Script Function?
what is the need of the Ajax?
In how many ways we can the thread? in java
Is char a data type in java?
What are the important features of Java 10 release?
5 What is Java exception handling?