Example usage for javax.swing.tree DefaultMutableTreeNode getChildAt

List of usage examples for javax.swing.tree DefaultMutableTreeNode getChildAt

Introduction

In this page you can find the example usage for javax.swing.tree DefaultMutableTreeNode getChildAt.

Prototype

public TreeNode getChildAt(int index) 

Source Link

Document

Returns the child at the specified index in this node's child array.

Usage

From source file:Main.java

public static void expandTreeNode(JTree tree, DefaultMutableTreeNode node, int depth) {
    if (depth <= 0) {
        return;/*  w w  w.ja v  a2  s . c o m*/
    }
    tree.expandPath(new TreePath(node.getPath()));
    for (int i = 0; i < node.getChildCount(); i++) {
        expandTreeNode(tree, (DefaultMutableTreeNode) node.getChildAt(i), depth - 1);
    }
}

From source file:net.sf.taverna.t2.workbench.ui.workflowexplorer.WorkflowExplorerTreeCellRenderer.java

private static boolean hasGrandChildren(DefaultMutableTreeNode node) {
    int childCount = node.getChildCount();
    for (int i = 0; i < childCount; i++)
        if (node.getChildAt(i).getChildCount() > 0)
            return true;
    return false;
}

From source file:Main.java

public static DefaultMutableTreeNode cloneTreeNode(DefaultMutableTreeNode srcNode) {
    if (srcNode == null)
        return null;
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(srcNode.getUserObject());
    for (int i = 0; i < srcNode.getChildCount(); i++) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) srcNode.getChildAt(i);
        addCloneNode(child, node);/*from ww  w  . j a  v a 2  s . c  o m*/
    }
    return node;
}

From source file:Main.java

private static DefaultMutableTreeNode addCloneNode(DefaultMutableTreeNode srcNode,
        DefaultMutableTreeNode root) {
    DefaultMutableTreeNode clone = new DefaultMutableTreeNode(srcNode.getUserObject());
    root.add(clone);//from w  w  w  .  j a va  2 s.c om
    for (int i = 0; i < srcNode.getChildCount(); i++) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) srcNode.getChildAt(i);
        addCloneNode(child, clone);
    }
    return clone;
}

From source file:Main.java

public static DefaultMutableTreeNode sortTreeNode(DefaultMutableTreeNode root) {
    if (root == null)
        return root;
    int count = root.getChildCount();
    if (count <= 0)
        return root;
    for (int i = 0; i < count; i++) {
        for (int j = count - 1; j > i; j--) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(j);
            String nt = node.getUserObject().toString();
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j - 1);
            String np = prevNode.getUserObject().toString();
            if (nt.compareToIgnoreCase(np) < 0) {
                root.insert(node, j - 1);
                root.insert(prevNode, j);
            }/*from   www  .  j  a v a2s. c  o  m*/
        }
        sortTreeNode((DefaultMutableTreeNode) root.getChildAt(i));
    }

    for (int i = 0; i < count; i++) {
        for (int j = count - 1; j > i; j--) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(j);
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j - 1);
            if (prevNode.isLeaf() && !node.isLeaf()) {
                root.insert(node, j - 1);
                root.insert(prevNode, j);
            }
        }
    }

    return root;
}

From source file:OAT.ui.util.UiUtil.java

public static DefaultMutableTreeNode addChildNode(DefaultMutableTreeNode parent, Object child) {
    HashSet hashSet = new HashSet();
    DefaultMutableTreeNode childNode;

    for (int i = 0; i < parent.getChildCount(); i++) {
        hashSet.add(parent.getChildAt(i).toString());
    }/*from  w  ww .  j a  va 2 s . c o m*/

    if (hashSet.add(child.toString())) {
        childNode = new DefaultMutableTreeNode(child);
        parent.insert(childNode, parent.getChildCount());
    } else {
        int i = 0;
        do {
            childNode = (DefaultMutableTreeNode) parent.getChildAt(i++);
            //System.out.println("childNode: " + childNode);
        } while (!childNode.toString().equals(child.toString()));
    }

    return childNode;
}

From source file:com.openbravo.pos.admin.RolesViewTree.java

public static DefaultMutableTreeNode sortTree(DefaultMutableTreeNode root) {
    for (int i = 0; i < root.getChildCount() - 1; i++) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
        String nt = node.getUserObject().toString();
        for (int j = i + 1; j <= root.getChildCount() - 1; j++) {
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j);
            String np = prevNode.getUserObject().toString();
            if (nt.compareToIgnoreCase(np) > 0) {
                root.insert(node, j);/*  w  ww .  j a va2 s.c  o m*/
                root.insert(prevNode, i);
            }
        }
        if (node.getChildCount() > 0) {
            node = sortTree(node);
        }
    }
    return root;
}

From source file:TreeUtil.java

public static TreePath makeTreePath(String path, DefaultMutableTreeNode parentNode) {
    DefaultMutableTreeNode tempNode = parentNode;
    TreePath res = new TreePath(parentNode);
    StringTokenizer tok = new StringTokenizer(path, ".");
    String currentPath = null;// w w w  . j ava  2  s.co  m
    while (tok.hasMoreTokens()) {
        String myTok = tok.nextToken();
        currentPath = (currentPath == null) ? myTok : currentPath + "." + myTok;
        for (int j = 0; j < tempNode.getChildCount(); j++) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) tempNode.getChildAt(j);
            if (childNode.toString().equals(myTok)) {
                tempNode = childNode;
                res = res.pathByAddingChild(tempNode);
                break;
            }
        }
    }
    return res;
}

From source file:Main.java

private void initializeButtons(DefaultMutableTreeNode node) {
    Button b;/*w  ww  .  j a  va2  s. c  o  m*/
    buttonPanel.removeAll();
    for (int i = 0; i < node.getChildCount(); i++) {
        b = new Button();
        b.setLabel("" + node.getChildAt(i));
        buttonPanel.add(b);
        buttonPanel.revalidate();
    }
}

From source file:MyTreeModelListener.java

public void treeNodesChanged(TreeModelEvent e) {
    DefaultMutableTreeNode node;
    node = (DefaultMutableTreeNode) (e.getTreePath().getLastPathComponent());
    try {//from  ww w  .  j a  v  a 2s.c  o  m
        int index = e.getChildIndices()[0];
        node = (DefaultMutableTreeNode) (node.getChildAt(index));
    } catch (NullPointerException exc) {
    }
    System.out.println("New value: " + node.getUserObject());
}