Answer Posted / yuvaraaj
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 |
Post New Answer View All Answers
What are the legal parameters?
What is unmodifiable collection in java?
How many characters is 2 bytes?
What is string substring?
What is the do while loop syntax?
Differentiate between the constructors and methods in java?
Can we make a constructor final?
What is array list in java?
What is immutable data?
Java Compiler is stored in JDK, JRE or JVM?
What are methods and how are they defined?
What are the steps involved to create a bean?
Explain about OOPS concepts and fundamentals.
What is a cup of java?
When is an object subject to garbage collection?