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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}

Donations appreciated. Every little $ helps. Or click Google +1

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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:

1
JTreeTool.expandPagingLeaf(tree, JTreeTool.NEXT)

Donations appreciated. Every little $ helps. Or click Google +1

Java: Expand First Leaf Node Of JTree

Use this helper method that I created to expand the first leaf node of a JTree object. By default, only the child nodes of the root are displayed. Some cases, you may want to display the very first leaf node of a JTree object.

1
2
3
4
5
public static void expandAndSelectFirstLeaf(JTree tree) {
  TreePath tp = tree.getPathForRow(0);
  DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) tp.getLastPathComponent();
  tree.setSelectionPath(new TreePath(dmtn.getFirstLeaf().getPath()));
}

This method assumes that the first child node of the root has a child of its own. This method also does not care how deep the TreePath is. It will expand all nodes of the TreePath until the leaf node.

Donations appreciated. Every little $ helps. Or click Google +1

Related Posts with Thumbnails