I've got a JTree with a custom model which extends DefaultTreeModel. I need to be able to move a node from one branch to a different branch without losing the selection.
Currently, I'm doing it in my model like this:
private void moveNode( MutableTreeNode node, MutableTreeNode newParent ) {
super.removeNodeFromParent( node );
super.insertNodeInto( node, newParent, 0 );
}
Since I'm using the DefaultTableModel methods, the node gets to the right place and the tree view gets updated, but it also loses the selection on the node. That makes sense, since it's being (temporarily) removed but it's not the behavior I want.
What's the correct way to move a node like this? The selection after the move should be the same as before the move, whether that involves re-selecting the node after it's moved or not.