Create A JTree The Easy Way Using Java

I forgot where I got this code but I am posting it here for others who may want to have an easy way to use a JTree. Rather than doing lines of codes for adding nodes, you can use this class (I call it EasyJTree) to add notes using String names. Here is the code.

public class EasyJTree extends JTree {
    //create map of names to node objects
    private HashMap namesToNodes = new HashMap();
 
    public EasyJTree() {
        //create root of tree
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
        this.setModel(new DefaultTreeModel(root));
 
        //add to map
        namesToNodes.put("root", root);
    }
 
    //get node object with name
    private DefaultMutableTreeNode getNode(String name) {
        //make lowercase
        //name = name.toLowerCase();
 
        //get node from map
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)(namesToNodes.get(name));
 
        //if not defined
        if(node == null) {
            //create new node
            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(name);
 
            //add to map
            namesToNodes.put(name, newNode);
 
            //return new node
            return newNode;
        } else {
            //return old node
            return node;
        }
    }
 
    public void expandRoot() {
        //expand path to root
        expandPath(new TreePath(getNode("root")));
    }
 
    //add first node to second node
    public void addNodeTo(String childNodeName, String parentNodeName) {
        //get two nodes
        DefaultMutableTreeNode parentNode = getNode(parentNodeName);
        DefaultMutableTreeNode childNode = getNode(childNodeName);
 
        //add child to parent at the end of child list
        ((DefaultTreeModel)getModel()).insertNodeInto(childNode, parentNode, parentNode.getChildCount());
    }
 
}

To use the EasyJTree class is pretty easy. Note that you must always supply the root node’s String value as “root”. Do not worry. The class’ root node is set to hidden by default but the code needs a root node so it can form the tree hierarchy properly.

EasyJTree tree = new EasyJTree();
tree.addNodeTo("Item1","root");
tree.addNodeTo("Sub Item 1","Item1");
tree.addNodeTo("Sub Item 2","Item1");
tree.addNodeTo("Item2","root");
tree.expandRoot();

Java: Get Leaf Node Index From Leaf Nodes In A JTree

I created this method to return the index of a leaf node from its leaf nodes in the JTree object. Using the TreePaths of the root and the leaf node to search for, the method loops through all the root’s contents until it encounters the leaf node equal to the one passed to the second parameter.

public static int getLeafIndex(TreePath tproot, TreePath tp) {
    int index = -1;
 
    DefaultMutableTreeNode dmtn_dummy = null;
    while (true) {
        if (dmtn_dummy == null) dmtn_dummy = (DefaultMutableTreeNode) tproot.getLastPathComponent();
        else dmtn_dummy = dmtn_dummy.getNextNode();
 
        if (dmtn_dummy != null) {
            if (dmtn_dummy.isLeaf()) {
                index++;
                if (new TreePath(dmtn_dummy.getPath()).equals(tp)) break;
            }
        } else break;
    }
    return index;
}

Java: Paging Leaf Nodes Of JTree

I made a method for a JTree object that will mimic paging features within its leaf nodes. If you have FIRST, NEXT, PREVIOUS and LAST buttons and you click say, NEXT, it will go to the next leaf node of the JTree. If the path to that leaf node is not expanded, it will expand it right away. This is useful in cases wherein you only want the navigation to affect leaf nodes.

The method has 2 parameters: first is the JTree object while the second will require you what kind of paging you want. Either the FIRST, NEXT, PREVIOUS and LAST leaf nodes of the JTree. Here is the code:

public final static int FIRST = 0, NEXT = 1, PREVIOUS = 2, LAST = 3;
 
public static boolean expandPagingLeaf(JTree tree, int paging) {
 
    TreePath tp = null;
    DefaultMutableTreeNode dmtn_dummy = null, dmtn = null, dmtn_prev = null;
 
    if (paging == FIRST) tp = tree.getPathForRow(0);
    else tp = tree.getSelectionPath();
 
    dmtn = (DefaultMutableTreeNode) tp.getLastPathComponent();
 
    while (true) {
        if (dmtn_dummy == null)
            if (paging == PREVIOUS) dmtn_dummy = dmtn.getPreviousNode();
            else dmtn_dummy = dmtn.getNextNode();
        else
            if (paging == PREVIOUS) dmtn_dummy = dmtn_dummy.getPreviousNode();
            else dmtn_dummy = dmtn_dummy.getNextNode();
 
        if (dmtn_dummy != null) {
            if (dmtn_dummy.isLeaf()) {
                if (isSameTreePath(new TreePath(dmtn_dummy.getPath()), tree.getSelectionPath())) return false;
                else if (paging != LAST) {
                    tree.setSelectionPath(new TreePath(dmtn_dummy.getPath()));
                    break;
                }
            }
            dmtn_prev = dmtn_dummy;
        } else {
            if (paging == LAST && dmtn_prev != null) {
                tree.setSelectionPath(new TreePath(dmtn_prev.getPath()));
                break;
            } else return false;
        }
    }
 
    tree.scrollPathToVisible(tree.getSelectionPath());
    return true;
}

To call the method to go to the next leaf node available, you can do it like this:

JTreeTool.expandPagingLeaf(tree, JTreeTool.NEXT)
Related Posts Plugin for WordPress, Blogger...