Jan
10
2008

Java Methods for TreeNodes



Comments available as RSS 2.0

Two pretty useful Java methods. They're both related to JTrees, but should have been included as built in methods really.

The first returns a treepath given an object. Once you have a JTree, you may need to get the path to the node that contains that object (to scroll the JTree to that object for example). It's a recursive function so it doesn't matter how many levels your JTree contains.

    private TreePath getPathToNode(DefaultMutableTreeNode node, Object obj) {
	System.out.println("Looking for "+obj.toString()+" in "+node.toString());
        if(node.toString().equals(obj.toString())) {
            return new TreePath(node.getPath());
        } else {
            for(Enumeration theChildren = node.children(); theChildren.hasMoreElements();) {
                DefaultMutableTreeNode thisNode = (DefaultMutableTreeNode)theChildren.nextElement();
                if(thisNode.toString().equals(obj.toString())) {
                    System.out.println("Found node at "+new TreePath(thisNode.getPath()));
                    return new TreePath(thisNode.getPath());
                } else if(thisNode.getChildCount()>0) {
                    for(Enumeration grandChildren = thisNode.children(); grandChildren.hasMoreElements();) {
                        DefaultMutableTreeNode grandChild = (DefaultMutableTreeNode)grandChildren.nextElement();
                        TreePath n = getPathToNode(grandChild, obj);
                        if(n != null) {
                            return n; // if the call returns anything but null, we've found the node
                        }
                    }
                }
            }
            System.out.println("Node not found");
            return null;
        }
    }

The second returns a DefaultMutableTreeNode given an object. It recursively searches the JTree until it finds the object matching the one you pass to it, then returns the node containing that object.

    private DefaultMutableTreeNode getNodeFromName(DefaultMutableTreeNode node, Object obj) {
        // Used to scroll the JTree to the current account
        for(Enumeration theChildren = node.children(); theChildren.hasMoreElements();) {
            DefaultMutableTreeNode thisNode = (DefaultMutableTreeNode)theChildren.nextElement();
            if(thisNode.toString().equals(obj.toString())) {
                return thisNode;
            } else {
                if(thisNode.getChildCount()>0) {
                    for(Enumeration grandChildren = thisNode.children(); grandChildren.hasMoreElements();) {
                        DefaultMutableTreeNode grandChild = (DefaultMutableTreeNode)grandChildren.nextElement();
                        DefaultMutableTreeNode n = getNodeFromName(grandChild, obj);
                        if(n != null) {
                            return n; // if the call returns anything but null, we've found the node
                        }
                    }
                }
            }
        }
        System.out.println("Node not found");
        return null;
    }

One thought on “Java Methods for TreeNodes

  1. Pingback: Toying With Swing « Dark Views

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">