Java Utililty Methods JTree Path

List of utility methods to do JTree Path

Description

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

Method

StringcheckNode(TreePath path)
check Node
String type = "";
if (null != path) {
    TreePath parentPath = path.getParentPath();
    DefaultMutableTreeNode currNode = (DefaultMutableTreeNode) path.getLastPathComponent();
    DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) parentPath.getLastPathComponent();
    if (("My Flows".equals(currNode.getUserObject()) || "Company Flows".equals(currNode.getUserObject()))
            && parentNode.isRoot()) {
        type = ADD_FOLDER_NEW_FLOW;
...
TreePathcreateTreePath(TreeNode treeNode)
create Tree Path
List<TreeNode> list = new ArrayList<TreeNode>();
list.add(treeNode);
TreeNode parent = treeNode.getParent();
while (parent != null) {
    list.add(0, parent);
    parent = parent.getParent();
return new TreePath(list.toArray());
...
TreePathcreateTreePathFromTreeNode(TreeNode treeNode)
create Tree Path From Tree Node
ArrayList<TreeNode> list = new ArrayList<TreeNode>();
do {
    list.add(treeNode);
    treeNode = treeNode.getParent();
} while (treeNode != null);
Collections.reverse(list);
return new TreePath(list.toArray(new Object[list.size()]));
voiddoNode(JTree tree, TreeNode parentNode, TreePath parentPath)
do Node
if (parentNode.getChildCount() > 0) {
    for (Enumeration e = parentNode.children(); e.hasMoreElements();) {
        TreeNode childNode = (TreeNode) e.nextElement();
        TreePath path = parentPath.pathByAddingChild(childNode);
        tree.expandPath(path);
        doNode(tree, childNode, path);
TreePathfind2(JTree tree, TreePath parent, Object[] nodes, int depth, boolean byName)
find
TreeNode node = (TreeNode) parent.getLastPathComponent();
Object o = node;
if (o == null)
    return null;
if (byName) {
    o = o.toString();
System.err.println(o + " : " + nodes[depth]);
...
DefaultMutableTreeNodefindOrAddNode(JTree tree, TreePath parent, String packageName)
find Or Add Node
final DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getLastPathComponent();
for (final Enumeration e = node.children(); e.hasMoreElements();) {
    final DefaultMutableTreeNode n = (DefaultMutableTreeNode) e.nextElement();
    if (packageName.equals(n.toString()))
        return n;
final DefaultMutableTreeNode nodeCls = new DefaultMutableTreeNode(packageName);
node.add(nodeCls);
...
TreePathfindTreePath(TreePath path, TreeNode node, String pathName)
find Tree Path
if (path == null || pathName == null || node == null) {
    return null;
if (pathName.equals(getTreePathString(path))) {
    return path;
if (node.getChildCount() >= 0) {
    for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
...
DefaultMutableTreeNodefindV2MetaTreeNode(String nodeXmlPath)
find V Meta Tree Node
return elementNodehash.get(nodeXmlPath);
intgetDepth(JTree tree)
get Depth
TreeNode root = (TreeNode) tree.getModel().getRoot();
if (root.getChildCount() == 0)
    return 0;
else
    return 1 + getDepth(root.getChildAt(0));
EnumerationgetExpatnedTreePaths(JTree tree)
Method to be used in conjunction with #expandTreePaths(JTree,Enumeration)
TreePath pathToRoot = new TreePath(tree.getModel().getRoot());
Enumeration<TreePath> expandPaths = tree.getExpandedDescendants(pathToRoot);
return expandPaths;