Example usage for javax.swing.tree DefaultMutableTreeNode getChildCount

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

Introduction

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

Prototype

public int getChildCount() 

Source Link

Document

Returns the number of children of this node.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame(Main.class.getSimpleName());
    DefaultTreeModel model = getTreeModel();
    JTree tree1 = new JTree(model);
    JTree tree2 = new JTree(model);
    frame.add(new JScrollPane(tree1), BorderLayout.WEST);
    frame.add(new JScrollPane(tree2), BorderLayout.EAST);
    frame.pack();//from   www.  java2  s.c  om
    frame.setSize(frame.getWidth() + 50, frame.getHeight() + 140);
    frame.setVisible(true);
    Timer t = new Timer(2000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
            root.add(new DefaultMutableTreeNode("A new node"));
            model.nodesWereInserted(root, new int[] { root.getChildCount() - 1 });
            tree1.expandRow(0);
            tree2.expandRow(0);
            frame.revalidate();
        }
    });
    t.start();
}

From source file:Main.java

public static void main(String[] args) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    DefaultTreeModel model = new DefaultTreeModel(root);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(new JTree(model)));
    f.getContentPane().add(new JButton(new AbstractAction("Add thousand children") {
        @Override//from   w  w  w  .j ava 2  s .  c om
        public void actionPerformed(ActionEvent e) {
            int offset = root.getChildCount() + 1;
            for (int i = 0; i < 1000; i++) {
                DefaultMutableTreeNode child = new DefaultMutableTreeNode("Person " + (i + offset));
                // model.insertNodeInto(child, root, root.getChildCount());
                root.add(child);
            }
            model.nodeStructureChanged(root);
        }
    }), BorderLayout.PAGE_END);
    f.pack();
    f.setVisible(true);
}

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 av a 2 s . 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:Main.java

public static void expandTreeNode(JTree tree, DefaultMutableTreeNode node, int depth) {
    if (depth <= 0) {
        return;//from  w w  w .jav a  2s  . 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: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 w w  w.j  av a  2 s.  c  o m*/
    }
    return node;
}

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

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

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 va2 s . c om*/

    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 .  ja v  a  2  s. com*/
                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;//from w w  w  .  j a  va2  s.c o 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;
}