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 do you mean by mnemonics?
What's the difference between an abstract class and interface in java?
Why destructor is not used in java?
How do you replace a string in java?
How to add and remove nodes in Jtree?
Difference between static and dynamic class loading.
What is the difference between the direct buffer and non-direct buffer in java?
What are methods?
What are the steps in the jdbc connection?
How we get some middle records in one table?
What is the use of :: in java?
What is static binding and where it occurs?