Results 1 to 3 of 3
- 12-22-2010, 02:14 AM #1
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 35
- Rep Power
- 0
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: JTree Programmatic Node Expansion and Selection ProbelmLast edited by PrinceSendai; 12-22-2010 at 02:17 AM.
- 12-22-2010, 04:12 AM #2
Use code tags to post code. [code]CODE[/code] will display as
In future, for such problems, post a SSCCE.Java Code:CODE
SSCCE : Java Glossary
Assuming the code is in a method of its own, that doesn't need to do anything else:dbJava Code:DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (node == null) { return; } TreeNode previousSibling = node.getPreviousSibling(); TreeNode nextNode; if (previousSibling != null) { nextNode = previousSibling; } else { nextNode = node.getParent(); } model.removeNodeFromParent(node); if (nextNode == root) { return; } TreeNode[] nodes = model.getPathToRoot(nextNode); tree.setSelectionPath(new TreePath(nodes));Last edited by DarrylBurke; 12-22-2010 at 04:16 AM. Reason: Shortened the code and removed unnecessary casting
- 12-22-2010, 05:34 AM #3
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 35
- Rep Power
- 0
Thank you for the correct and clear answer. Worked perfectly, and hope other people can find this answer if they encounter my problem.
I will also consider your tips on code formatting for future posts.
Thanks again (saved me a lot of time),
PSLast edited by PrinceSendai; 12-22-2010 at 05:36 AM.
Similar Threads
-
Highlighting with exact match
By ashkandaie in forum LuceneReplies: 3Last Post: 09-09-2010, 06:36 AM -
Need help with highlighting text in a jcombobox
By cjmartin in forum AWT / SwingReplies: 3Last Post: 08-09-2010, 06:21 PM -
Eclipse Syntax Highlighting
By garrettgjb in forum EclipseReplies: 1Last Post: 08-24-2009, 03:10 PM -
Move JTree item to another JTree.
By Melki in forum AWT / SwingReplies: 8Last Post: 07-09-2009, 11:59 AM -
[SOLVED] Line Highlighting
By Doctor Cactus in forum New To JavaReplies: 2Last Post: 03-26-2009, 03:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks