The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.tree  [15 examples] > Nodes  [6 examples]

e1024. Finding a Node in a JTree Component

    // Create tree
    JTree tree = new JTree();
    
    // Search forward from first visible row looking for any visible node
    // whose name starts with prefix.
    int startRow = 0;
    String prefix = "b";
    TreePath path = tree.getNextMatch(prefix, startRow, Position.Bias.Forward);
    
    // Search backward from last visible row looking for any visible node
    // whose name starts with prefix.
    startRow = tree.getRowCount()-1;
    prefix = "b";
    path = tree.getNextMatch(prefix, startRow, Position.Bias.Backward);
    
    // Find the path (regardless of visibility) that matches the
    // specified sequence of names
    path = findByName(tree, new String[]{"JTree", "food", "bananas"});
    
    // Finds the path in tree as specified by the node array. The node array is a sequence
    // of nodes where nodes[0] is the root and nodes[i] is a child of nodes[i-1].
    // Comparison is done using Object.equals(). Returns null if not found.
    public TreePath find(JTree tree, Object[] nodes) {
        TreeNode root = (TreeNode)tree.getModel().getRoot();
        return find2(tree, new TreePath(root), nodes, 0, false);
    }
    
    // Finds the path in tree as specified by the array of names. The names array is a
    // sequence of names where names[0] is the root and names[i] is a child of names[i-1].
    // Comparison is done using String.equals(). Returns null if not found.
    public TreePath findByName(JTree tree, String[] names) {
        TreeNode root = (TreeNode)tree.getModel().getRoot();
        return find2(tree, new TreePath(root), names, 0, true);
    }
    private TreePath find2(JTree tree, TreePath parent, Object[] nodes, int depth, boolean byName) {
        TreeNode node = (TreeNode)parent.getLastPathComponent();
        Object o = node;
    
        // If by name, convert node to a string
        if (byName) {
            o = o.toString();
        }
    
        // If equal, go down the branch
        if (o.equals(nodes[depth])) {
            // If at end, return match
            if (depth == nodes.length-1) {
                return parent;
            }
    
            // Traverse children
            if (node.getChildCount() >= 0) {
                for (Enumeration e=node.children(); e.hasMoreElements(); ) {
                    TreeNode n = (TreeNode)e.nextElement();
                    TreePath path = parent.pathByAddingChild(n);
                    TreePath result = find2(tree, path, nodes, depth+1, byName);
                    // Found a match
                    if (result != null) {
                        return result;
                    }
                }
            }
        }
    
        // No match at this branch
        return null;
    }

 Related Examples
e1023. Visiting All the Nodes in a JTree Component
e1025. Adding a Node to a JTree Component
e1026. Removing a Node to a JTree Component
e1027. Converting a Node in a JTree Component to a TreePath
e1028. Converting All Nodes in a JTree Component to a TreePath Array

See also: Events    Layout    Node Expansion    Selections   


© 2002 Addison-Wesley.