Reputation: 28312
Right now, I have a TreeSelectionListener
on my JTree
. This works fine. However, I need to detect the case that a node has been deleted. I am not sure how to do this using a TreeSelectionEvent.
I haven't seen anything in the JavaDoc about it. I was looking for something analogous to a TableModelEvent
where it has a getType()
method that allows you to know if something was updated,deleted, or inserted into the table. Any idea how this can be detected?
I can't use a TreeModelListener, because when I call:
((DefaultTreeModel)getModel()).removeNodeFromParent(node);
On my tree, it triggers the valueChanged(TreeSelectionEvent)
to be fired. This is what I am trying to escape from.
Something like:
public void valueChanged(TreeSelectionEvent treeSelectionEvent){
if(treeSelectionEvent.someMethod()){ <----need this
return;
}
}
Upvotes: 0
Views: 125
Reputation: 8348
I need to detect the case that a node has been deleted
To detect that a node has been deleted, you can use a TreeModelListener
. Simple example:
DefaultTreeModel model = new DefaultTreeModel(new DefaultMutableTreeNode("Root"));
JTree tree = new JTree(model);
model.addTreeModelListener(new TreeModelListener(){
public void treeNodesRemoved(TreeModelEvent e){
//do something
}
//further listener implementation here
});
EDIT: To prevent the Selection listener from firing when deleting the node, you could
Upvotes: 3