Wednesday, April 10, 2002 07:15 AM
It is going to depend on how you did the TreeModel. Here are some what-if's:
If you are using DefaultTreeModel with DefaultMutableTreeNode nodes, you find the node you want to remove (call it 'node'), which is a DefaultMutableTreeNode and do something like the code below:
Object path = ((DefaultMutableTreeNode)node.getParent()).getPath(); // You can build this path yourself
int childIndices = new int;
childIndices = node.getParent().getIndexInParent(node); // index of removed node (before being removed)
Object children = new Object;
children = node; // the removed node
node.removeFromParent(); // take out the node from the tree.
tree.getModel().fireTreeNodesRemoved(this, path, childIndices, children); // Fire the change event
If you are using some other TreeModel, the principle is the same.
- Find the path to the parent of the node being removed.
- Find the index of the removed node/object in that parent's children
- You know the object being removed 4) remove the object you want to remove from whereever it is stored. Do so in a way that it will not be retreived by the TreeModel in the future.
- Issue a TreeModelEvent to all listeners attached to the TreeModel. Call their 'treeModelRemoved' method with the TreeModelEvent build from the information in 1, 2 and 3.
Without more information about your jtree, that's vague, but the best I can do.