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 Main() {
    DefaultMutableTreeNode forums = new DefaultMutableTreeNode("B");
    forums.add(new DefaultMutableTreeNode("T"));
    DefaultMutableTreeNode articles = new DefaultMutableTreeNode("A");
    articles.add(new DefaultMutableTreeNode("A"));
    DefaultMutableTreeNode examples = new DefaultMutableTreeNode("E");
    examples.add(new DefaultMutableTreeNode("E"));

    rootNode.add(forums);//  ww w  .jav a 2  s . co  m
    rootNode.add(articles);
    rootNode.add(examples);

    m_tree.setEditable(true);
    m_tree.setSelectionRow(0);

    JScrollPane scrollPane = new JScrollPane(m_tree);
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    JPanel panel = new JPanel();
    m_addButton = new JButton("Add Node");
    m_addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();

            if (selNode == null) {
                return;
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");
            m_model.insertNodeInto(newNode, selNode, selNode.getChildCount());

            TreeNode[] nodes = m_model.getPathToRoot(newNode);
            TreePath path = new TreePath(nodes);
            m_tree.scrollPathToVisible(path);

            m_tree.setSelectionPath(path);

            m_tree.startEditingAtPath(path);
        }

    });
    panel.add(m_addButton);
    getContentPane().add(panel, BorderLayout.SOUTH);
    m_delButton = new JButton("Delete Node");
    m_delButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();
            if (selNode == null) {
                return;
            }
            MutableTreeNode parent = (MutableTreeNode) (selNode.getParent());
            if (parent == null) {
                return;
            }
            MutableTreeNode toBeSelNode = (MutableTreeNode) selNode.getPreviousSibling();
            if (toBeSelNode == null) {
                toBeSelNode = (MutableTreeNode) selNode.getNextSibling();
            }
            if (toBeSelNode == null) {
                toBeSelNode = parent;
            }
            TreeNode[] nodes = m_model.getPathToRoot(toBeSelNode);
            TreePath path = new TreePath(nodes);
            m_tree.scrollPathToVisible(path);
            m_tree.setSelectionPath(path);
            m_model.removeNodeFromParent(selNode);
        }

    });
    panel.add(m_delButton);
    getContentPane().add(panel, BorderLayout.SOUTH);

    setSize(300, 400);
    setVisible(true);
}

From source file:edu.ku.brc.specify.tasks.subpane.security.NavigationTreeContextMenuMgr.java

/**
 * @param node// w ww .ja va 2 s.  com
 * @return
 */
private DefaultMutableTreeNode getAdminTreeNode(final DefaultMutableTreeNode node) {
    for (int i = 0; i < node.getChildCount(); i++) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
        FormDataObjIFace dmObj = ((DataModelObjBaseWrapper) child.getUserObject()).getDataObj();

        if (dmObj instanceof SpPrincipal) {
            String name = ((SpPrincipal) dmObj).getName();
            if (name.equals("Administrator")) {
                return (DefaultMutableTreeNode) child;
            }
        }
        DefaultMutableTreeNode cNode = getAdminTreeNode(child);
        if (cNode != null) {
            return cNode;
        }
    }
    return null;
}

From source file:fi.elfcloud.client.tree.ClusterHierarchyModel.java

public void addNode(DefaultMutableTreeNode root, Cluster c) {
    ClusterNode clusterNode = new ClusterNode(c);
    insertNodeInto(clusterNode, root, root.getChildCount());
}

From source file:EditableTree.java

public EditableTree() {
    DefaultMutableTreeNode forums = new DefaultMutableTreeNode("A");
    forums.add(new DefaultMutableTreeNode("B"));
    DefaultMutableTreeNode articles = new DefaultMutableTreeNode("E");
    articles.add(new DefaultMutableTreeNode("F"));
    DefaultMutableTreeNode examples = new DefaultMutableTreeNode("G");
    examples.add(new DefaultMutableTreeNode("H"));

    m_rootNode.add(forums);//from www  .  ja v  a2 s  .c o m
    m_rootNode.add(articles);
    m_rootNode.add(examples);

    m_tree.setEditable(true);
    m_tree.setSelectionRow(0);

    JScrollPane scrollPane = new JScrollPane(m_tree);
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    JPanel panel = new JPanel();

    m_addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();
            if (selNode == null) {
                return;
            }
            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node");
            m_model.insertNodeInto(newNode, selNode, selNode.getChildCount());

            TreeNode[] nodes = m_model.getPathToRoot(newNode);
            TreePath path = new TreePath(nodes);
            m_tree.scrollPathToVisible(path);
            m_tree.setSelectionPath(path);
            m_tree.startEditingAtPath(path);
        }
    });
    panel.add(m_addButton);

    m_delButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();
            removeNode(selNode);
        }
    });
    panel.add(m_delButton);

    JPanel searchPanel = new JPanel();
    searchPanel.setBorder(BorderFactory.createEtchedBorder());

    m_searchText = new JTextField(10);
    searchPanel.add(m_searchText);

    m_searchButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode node = searchNode(m_searchText.getText());
            if (node != null) {
                TreeNode[] nodes = m_model.getPathToRoot(node);
                TreePath path = new TreePath(nodes);
                m_tree.scrollPathToVisible(path);
                m_tree.setSelectionPath(path);
            } else {
                System.out.println("Node with string " + m_searchText.getText() + " not found");
            }
        }
    });
    searchPanel.add(m_searchButton);

    m_searchAndDeleteButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode node = searchNode(m_searchText.getText());
            if (node != null) {
                removeNode(node);
            } else {
                System.out.println("Node with string " + m_searchText.getText() + " not found");
            }
        }
    });
    searchPanel.add(m_searchAndDeleteButton);
    panel.add(searchPanel);
    getContentPane().add(panel, BorderLayout.SOUTH);
    setSize(700, 400);
    setVisible(true);
}

From source file:appletComponentArch.DynamicTree.java

public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) {
    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);

    if (parent == null) {
        parent = rootNode;/*from   w  ww. ja va  2  s . c o m*/
    }

    //It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
    treeModel.insertNodeInto(childNode, parent, parent.getChildCount());

    //Make sure the user can see the lovely new node.
    if (shouldBeVisible) {
        tree.scrollPathToVisible(new TreePath(childNode.getPath()));
    }
    return childNode;
}

From source file:ClassTree.java

/**
 * Adds a new class and any parent classes that aren't yet part of the tree
 * @param c the class to add/*ww w  .j a  v  a  2  s  .com*/
 * @return the newly added node.
 */
public DefaultMutableTreeNode addClass(Class<?> c) {
    // add a new class to the tree

    // skip non-class types
    if (c.isInterface() || c.isPrimitive())
        return null;

    // if the class is already in the tree, return its node
    DefaultMutableTreeNode node = findUserObject(c);
    if (node != null)
        return node;

    // class isn't present--first add class parent recursively

    Class<?> s = c.getSuperclass();

    DefaultMutableTreeNode parent;
    if (s == null)
        parent = root;
    else
        parent = addClass(s);

    // add the class as a child to the parent
    DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(c);
    model.insertNodeInto(newNode, parent, parent.getChildCount());

    // make node visible
    TreePath path = new TreePath(model.getPathToRoot(newNode));
    tree.makeVisible(path);

    return newNode;
}

From source file:ListAlgorithmsBC.java

private void getNodes(DefaultMutableTreeNode providerNode, Provider provider, Set<Provider.Service> used,
        String title, String target) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(title);
    for (Provider.Service service : provider.getServices()) {
        if (!used.contains(service) && target.equalsIgnoreCase(service.getType())) {
            used.add(service);//from  ww  w.j  a  va2s .  co m
            DefaultMutableTreeNode algNode = new DefaultMutableTreeNode(service.getAlgorithm());
            node.add(algNode);
            algNode.add(new DefaultMutableTreeNode("class : " + service.getClassName()));
        }
    }
    if (node.getChildCount() != 0) {
        providerNode.add(node);
    }
}

From source file:com.pironet.tda.AbstractDumpParser.java

/**
 * retrieve the next node for adding histogram information into the tree.
 *
 * @param root the root to use for search.
 * @return node to use for append.//from  w  w  w . ja  va2  s . c  o m
 */
protected DefaultMutableTreeNode getNextDumpForHistogram(DefaultMutableTreeNode root) {
    if (dumpHistogramCounter == -1) {
        // -1 as index starts with 0.
        dumpHistogramCounter = root.getChildCount() - 1;
    }
    DefaultMutableTreeNode result = null;

    if (dumpHistogramCounter > 0) {
        result = (DefaultMutableTreeNode) root.getChildAt(dumpHistogramCounter);
        dumpHistogramCounter--;
    }

    return result;
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TreeNode root = getNodes();/*from w  w w.  j a  v a2  s .  com*/
    DefaultTreeModel model = new DefaultTreeModel(root);
    JTree tree = new JTree(model);
    tree.setRootVisible(false);

    JButton add = new JButton("add new");
    add.addActionListener(e -> {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (selectedNode == null) {
            return;
        }
        MyObject obj = (MyObject) selectedNode.getUserObject();
        MyObject newChild = new MyObject(obj.name + "-" + (obj.childs.size() + 1));
        obj.childs.add(newChild);
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(newChild);
        model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
        TreeNode[] nodes = model.getPathToRoot(newNode);
        TreePath path = new TreePath(nodes);
        tree.scrollPathToVisible(path);
    });

    JButton print = new JButton("print childs");
    print.addActionListener(e -> {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (selectedNode == null) {
            return;
        }
        MyObject obj = (MyObject) selectedNode.getUserObject();
        System.out.println(obj.childs);
    });
    JPanel btns = new JPanel();
    btns.add(add);
    btns.add(print);

    add(new JScrollPane(tree));
    add(btns, BorderLayout.SOUTH);
    pack();
    setVisible(true);
}

From source file:edu.ku.brc.specify.config.DebugLoggerDialog.java

protected void buildTree(final Logger logger, DefaultMutableTreeNode parent, final String[] names,
        final int level) {
    for (int i = 0; i < parent.getChildCount(); i++) {
        LoggerNode node = (LoggerNode) parent.getChildAt(i);
        //System.out.println("["+node.toString()+"]["+names[level]+"]");
        if (node.toString().equals(names[level])) {
            buildTree(logger, node, names, level + 1);
            return;
        }/* w  ww .jav a2s. c o  m*/
    }

    LoggerNode newNode = new LoggerNode(logger, names[level]);
    newNode.setSelected(logger.getLevel() == Level.DEBUG);
    parent.add(newNode);
    newNode.setParent(parent);
    if (level < names.length - 1) {
        buildTree(logger, newNode, names, level + 1);
    }
}