Results 1 to 3 of 3
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Angry 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 Probelm
    Last edited by PrinceSendai; 12-22-2010 at 02:17 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Use code tags to post code. [code]CODE[/code] will display as
    Java Code:
    CODE
    In future, for such problems, post a SSCCE.
    SSCCE : Java Glossary

    Assuming the code is in a method of its own, that doesn't need to do anything else:
    Java 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));
    db
    Last edited by DarrylBurke; 12-22-2010 at 04:16 AM. Reason: Shortened the code and removed unnecessary casting

  3. #3
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default

    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),
    PS
    Last edited by PrinceSendai; 12-22-2010 at 05:36 AM.

Similar Threads

  1. Highlighting with exact match
    By ashkandaie in forum Lucene
    Replies: 3
    Last Post: 09-09-2010, 06:36 AM
  2. Need help with highlighting text in a jcombobox
    By cjmartin in forum AWT / Swing
    Replies: 3
    Last Post: 08-09-2010, 06:21 PM
  3. Eclipse Syntax Highlighting
    By garrettgjb in forum Eclipse
    Replies: 1
    Last Post: 08-24-2009, 03:10 PM
  4. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  5. [SOLVED] Line Highlighting
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 03-26-2009, 03:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •