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 |
Which package has light weight components in java programming?
What is the difference between Access Modifier and Access specifier?
If try block is successfully executed, Then Is Finally block executed?
Why are arrays useful in java?
how to fing linkedlist is circular or not?
What is the concept of multithreading?
What do you understand by soft reference?
class A { public void disp(int a,int b) { System.out.println("hai"); } } class B { public void disp(int a,int b,int c) { System.out.println("hai"); } } above program is overloading or overriding?
Is null or empty java?
What is the difference between a constructor and a method?
What are the high-level thread states in java programming?
Which class is the superclass for all the classes?