Results 1 to 4 of 4
  1. #1
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default JTree Programmatic Node Expansion and Selection Probelm

    I've read plenty of java Swing forums or questions and none of them have the right answers. I am trying to dynamically select a node in a JTree by searching for it using a search panel based on its property.

    My jtree allows single selection model. (SINGLE_TREE_SELECTION).

    Java Code:
    jTree.setExpandsSelectedPaths(true);
    // jTree.expandPath(treePath);
    jTree.setSelectionPath(treePath);
    jTree.scrollPathToVisible(treePath);

    I know the node is being selected and a call to jTree.getSelectionPath() shows the expected treePath. The problem is that graphically the treePath is neither expanded nor the specific searched node is highlighted.

    TreePath is correct, and the node is being selected. The branch is just not being expanded and the node is not being highlighted. The node is not highlighted even when the branch is already in an expanded state.

    Can anyone please help.

    I am using Java 1.5

    Thanks,
    Bob

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Enumeration;
    import javax.swing.*;
    import javax.swing.tree.*;
    
    public class NodeExpansion implements ActionListener {
        JTree tree = new JTree();
    
        public void actionPerformed(ActionEvent e) {
            String s = e.getActionCommand();
            setExpandedState(s);
        }
    
        private void setExpandedState(String id) {
            DefaultMutableTreeNode root =
                (DefaultMutableTreeNode)tree.getModel().getRoot();
            Enumeration e = root.breadthFirstEnumeration();
            while(e.hasMoreElements()) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement();
                if(node.getUserObject().equals(id)) {
                    TreePath path = new TreePath(node.getPath());
                    if(node.isLeaf()) {
                        DefaultMutableTreeNode parent =
                            (DefaultMutableTreeNode)node.getParent();
                        expandNode(new TreePath(parent.getPath()));
                    } else {
                        expandNode(path);
                    }
                    tree.setSelectionPath(path);
                    break;
                }
            }
        }
    
        private void expandNode(TreePath parent) {
            TreeNode node = (TreeNode)parent.getLastPathComponent();
            if (node.getChildCount() >= 0) {
                Enumeration e = node.children();
                while(e.hasMoreElements()) {
                    TreeNode n = (TreeNode)e.nextElement();
                    TreePath path = parent.pathByAddingChild(n);
                    expandNode(path);
                }
            }
            tree.expandPath(parent);
        }
    
        private JPanel getContent() {
            JPanel panel = new JPanel(new GridLayout(1,0));
            panel.add(new JScrollPane(tree));
            panel.add(getRightComponent());
            return panel;
        }
    
        private JScrollPane getRightComponent() {
            ButtonGroup group = new ButtonGroup();
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2,5,2,0);
            gbc.weightx = 1.0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            TreeModel model = tree.getModel();
            DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
            Enumeration e = root.breadthFirstEnumeration();
            while(e.hasMoreElements()) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement();
                String s = node.getUserObject().toString();
                JRadioButton rb = new JRadioButton(s);
                rb.setActionCommand(s);
                rb.addActionListener(this);
                group.add(rb);
                panel.add(rb, gbc);
            }
            return new JScrollPane(panel);
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new NodeExpansion().getContent());
            f.setSize(500,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    Join Date
    Jan 2013
    Location
    new zealand
    Posts
    1
    Rep Power
    0

    Default Re: JTree Programmatic Node Expansion and Selection Probelm

    Hi Bob

    Have read your post but do not see any answer to your problem.

    I am in exactly the same position and have spent weeks on this problem with no solution. My tree path is correct, the "internal" tree structure has selected the required treepath node but the tree wont expand and display the selected path. I am stuck!! Have you found a solution? If so I would love to hear from you.

    Kind regards

    Karl

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: JTree Programmatic Node Expansion and Selection Probelm

    The question was asked more than 5 years ago, and the member hasn't been here for nearly 3 years.

    Don't post to old dead threads and don't hijack another poster's thread. When you have a question, start your own thread; they're free. But before you do that, go through a few links:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming
    SSCCE : Java Glossary

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 7
    Last Post: 05-09-2008, 07:54 AM
  2. How to Transmit data from one node to another
    By swimberl in forum Networking
    Replies: 2
    Last Post: 01-04-2008, 08:48 PM
  3. Gridbag Layout Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 12-04-2007, 05:12 PM
  4. Dom4j probelm with clearContent method
    By aorteu in forum New To Java
    Replies: 0
    Last Post: 11-23-2007, 03:36 PM
  5. Replies: 0
    Last Post: 07-25-2007, 09:04 AM

Posting Permissions

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