JTree setSelectionPath not highlighting
Hello,
I have been wanting to implement a very simple functionality:
Whenever I delete a node from the tree, I want my program to select the previous sibling node, and if there is no previous sibling, I want to select the parent.
I wrote the code for it and from what I can tell, my code does what it is supposed to EXCEPT that it doesn't highlight the selected node. In other words, the node is selected but not visibly highlighted.
Here is my code snippet ("node" is the node to delete):
DefaultTreeModel model = (DefaultTreeModel) dataTree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) dataTree.getLastSelectedPathComponent();
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) (node.getParent());
DefaultMutableTreeNode previousSibling = (DefaultMutableTreeNode) node.getPreviousSibling();
model.removeNodeFromParent(node);
if(previousSibling == null && parent != root){
dataTree.setSelectionPath(new TreePath(parent));
} else {
dataTree.setSelectionPath(new TreePath(previousSibling));
}
I found this question in many places online. However, in most cases the answer was to use setSelectionPath(new TreePath(node)) which is exactly what I am using. The only answer I saw which was different was one involving cell renderers and it was much longer than I would like. Since I don't really understand all the details about cell renderers and stuff I really would like to see if there is a simple solution to this issue. I mean, is this a java problem, that it does not show the node highlighted when it is in fact selected? I've spent the last 5 hours on this simple issue so if I am doing something wrong or there is a solution it will be greatly appreciated.
I actually found this same question on this forum but the answer response didn't work for me: http://www.java-forums.org/awt-swing...n-probelm.html