Java JTree Node getSubTree(TreeNode root, String name)

Here you can find the source of getSubTree(TreeNode root, String name)

Description

find a subtree in the root node and return it, based on exact match.

License

Open Source License

Parameter

Parameter Description
root the root
name the name

Return

the sub tree

Declaration

public static TreeNode getSubTree(TreeNode root, String name) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import javax.swing.tree.TreeNode;

public class Main {
    /**//from  w  w  w. j  av a  2  s  .  co  m
     * find a subtree in the root node and return it, based on exact match.
     *
     * @param root the root
     * @param name the name
     * @return the sub tree
     */
    public static TreeNode getSubTree(TreeNode root, String name) {
        // if root is what we are looking for, get it
        if (("" + root).equals(name))
            return root;
        // else go into children (depth-first)
        TreeNode node = null;
        for (int i = 0; i < root.getChildCount(); i++) {
            node = getSubTree(root.getChildAt(i), name);
            if (node != null)
                break;
        }
        return node;
    }
}

Related

  1. getLevel(TreeNode treeNode)
  2. getMutableTreeNodes(TreeNode[] path)
  3. getNodeAt(final JTree tree, final int x, final int y)
  4. getNodeForEvent(JTree targetTree, DropTargetDragEvent dtde)
  5. getPathStringForNode(DefaultMutableTreeNode node)
  6. hasOnlyLeafs(JTree tree, Object node)
  7. initializeExpandedPathsBeforeChange(JTree tree, DefaultMutableTreeNode root)
  8. insertNodeInAlphabeticalOrder(DefaultMutableTreeNode childNode, DefaultMutableTreeNode parentNode, DefaultTreeModel model)
  9. insertStrAsNodeLexi(DefaultTreeModel treeModel, String child, DefaultMutableTreeNode parent, Boolean childIsFile)