Java Utililty Methods JTree Node

List of utility methods to do JTree Node

Description

The list of methods to do JTree Node are organized into topic(s).

Method

voidexpandAll(JTree tree, DefaultMutableTreeNode treeNode)
expand All
Enumeration en = treeNode.breadthFirstEnumeration();
while (en.hasMoreElements()) {
    DefaultMutableTreeNode treeN = (DefaultMutableTreeNode) en.nextElement();
    tree.expandPath(new TreePath(treeN.getPath()));
voidexpandEntireTree(JTree tree, DefaultMutableTreeNode root, boolean expandRoot)
expand Entire Tree
if (expandRoot) {
    tree.expandPath(new TreePath(root.getPath()));
Enumeration<DefaultMutableTreeNode> children = root.children();
while (children.hasMoreElements()) {
    DefaultMutableTreeNode child = children.nextElement();
    tree.expandPath(new TreePath(child.getPath()));
    if (child.children().hasMoreElements()) {
...
voidexpandPathsAfterChange(JTree tree, Hashtable state, DefaultMutableTreeNode root)
Call this after the structure of a JTree changes to re-expand the paths.
TreePath path = new TreePath(root.getPath());
if (state.get(path.toString()) != null) {
    tree.expandPath(path);
for (int i = 0; i < root.getChildCount(); i++) {
    expandPathsAfterChange(tree, state, (DefaultMutableTreeNode) root.getChildAt(i));
voidexpandTree(final JTree tree, final DefaultMutableTreeNode node)
Fully expands the given JTree from the specified node.
if (node.isLeaf())
    return;
tree.expandPath(new TreePath(node.getPath()));
final Enumeration e = node.children();
while (e.hasMoreElements()) {
    expandTree(tree, (DefaultMutableTreeNode) e.nextElement());
voidexpandTreeRecurser(JTree tree, DefaultMutableTreeNode node)
expand Tree Recurser
TreePath path = getTreePathToChild(node);
tree.expandPath(path);
int childCount = node.getChildCount();
if (childCount != 0) {
    for (int x = 0; x < childCount; x++) {
        DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(x);
        expandTreeRecurser(tree, childNode);
voidexport(File file, DefaultMutableTreeNode node)
export
PrintStream fout = new PrintStream(file);
fout.println("#! /bin/sh\n");
for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
    DefaultMutableTreeNode child = (DefaultMutableTreeNode) e.nextElement();
    File sm = (File) child.getUserObject();
    fout.printf("rm -rf '%s'\n", sm.getAbsolutePath());
fout.print("\nrm -rf $0\n");
...
DefaultMutableTreeNodefindFirstTreeNodeMatchUserObject(DefaultMutableTreeNode treeNode, Object userObject)
This method will traverse the sub-tree whose root is itself and including itself to find a tree Node whose userObject matches the given one.
return null;
ListfindTreeNodesWithLowestLevel(List multipleTreeNodes)
Given a list of treeNodes, return those which are at the same level.
List result = new ArrayList();
int lowest = 99;
for (Iterator iter = multipleTreeNodes.iterator(); iter.hasNext();) {
    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) iter.next();
    int level = treeNode.getLevel();
    if (level < lowest) {
        lowest = level;
        result.clear();
...
DefaultMutableTreeNodefindTreeNodeWithXmlPath(DefaultMutableTreeNode treeNode, String nodeXmlPath)
find Tree Node With Xml Path
if (nodeXmlPath == null) {
    System.out.println("UIHelper.findTreeNodeWithXmlPath()..invalid node to search:" + nodeXmlPath);
    return null;
StringTokenizer st = new StringTokenizer(nodeXmlPath, "/@");
boolean foundRoot = false;
DefaultMutableTreeNode e = treeNode;
while (st.hasMoreTokens() && e != null) {
...
intgetAllChildCount(TreeNode node)
get All Child Count
if (node == null)
    return 0;
int count = 0;
int childCount = node.getChildCount();
for (int i = 0; i < childCount; i++) {
    count++;
    TreeNode child = node.getChildAt(i);
    count += getAllChildCount(child);
...