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

private void initializeButtons(DefaultMutableTreeNode node) {
    Button b;/* w w  w . j av  a  2 s  . c  om*/
    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:Main.java

/**
 * Method by Adrian: [/* w w  w.j  a  v a 2  s.  co  m*/
 * <a href="http://stackoverflow.com/a/15704264/5620200">StackOverflow</a> ]
 * & Mike: [ <a href=
 * "http://stackoverflow.com/questions/1542170/arranging-nodes-in-a-jtree">
 * StackOverflow</a> ]
 * 
 * @param node
 * @return
 */
@SuppressWarnings("unchecked")
public static DefaultMutableTreeNode sort(DefaultMutableTreeNode node) {
    List<DefaultMutableTreeNode> children = Collections.list(node.children());
    List<String> orgCnames = new ArrayList<String>();
    List<String> cNames = new ArrayList<String>();
    DefaultMutableTreeNode temParent = new DefaultMutableTreeNode();
    for (DefaultMutableTreeNode child : children) {
        DefaultMutableTreeNode ch = (DefaultMutableTreeNode) child;
        temParent.insert(ch, 0);
        String uppser = ch.toString().toUpperCase();
        // Not dependent on package name, so if duplicates are found
        // they will later on be confused. Adding this is of
        // very little consequence and fixes the issue.
        if (cNames.contains(uppser)) {
            uppser += "$COPY";
        }
        cNames.add(uppser);
        orgCnames.add(uppser);
        if (!child.isLeaf()) {
            sort(child);
        }
    }
    Collections.sort(cNames);
    for (String name : cNames) {
        int indx = orgCnames.indexOf(name);
        int insertIndex = node.getChildCount();
        node.insert(children.get(indx), insertIndex);
    }
    // Fixing folder placement
    for (int i = 0; i < node.getChildCount() - 1; i++) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
        for (int j = i + 1; j <= node.getChildCount() - 1; j++) {
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) node.getChildAt(j);
            if (!prevNode.isLeaf() && child.isLeaf()) {
                node.insert(child, j);
                node.insert(prevNode, i);
            }
        }
    }
    return node;
}

From source file:Main.java

protected void addFiles(File rootFile, DefaultTreeModel model, DefaultMutableTreeNode root) {
    for (File file : rootFile.listFiles()) {
        DefaultMutableTreeNode child = new DefaultMutableTreeNode(file);
        model.insertNodeInto(child, root, root.getChildCount());
        if (file.isDirectory()) {
            addFiles(file, model, child);
        }//  w  w  w . j  av a2s  .com
    }
}

From source file:Main.java

private void buildTreeFromString(final DefaultTreeModel model, final String str) {
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    String[] strings = str.split("/");
    DefaultMutableTreeNode node = root;
    for (String s : strings) {
        int index = childIndex(node, s);
        if (index < 0) {
            DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(s);
            node.insert(newChild, node.getChildCount());
            node = newChild;/*from w ww . j a v a  2  s .com*/
        } else {
            node = (DefaultMutableTreeNode) node.getChildAt(index);
        }
    }
}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);/* ww w. jav  a 2 s.  c o  m*/

    model = new DefaultTreeModel(top);
    tree = new JTree(model);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    JScrollPane treeView = new JScrollPane(tree);
    add(treeView);

    btnAdd.addActionListener(e -> {
        TreePath treePath = tree.getSelectionPath();
        if (treePath != null) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child " + (++childCount));
            model.insertNodeInto(child, node, node.getChildCount());
        }
    });
    add(btnAdd, BorderLayout.SOUTH);
}

From source file:AncestorTree.java

public boolean addAncestors(DefaultMutableTreeNode node) {
    if (node.getChildCount() > 0)
        return false;

    Object obj = node.getUserObject();
    if (obj == null)
        return false;
    node.add(new DefaultMutableTreeNode(new IconData(ICON_MALE, "Father of: " + obj.toString())));
    node.add(new DefaultMutableTreeNode(new IconData(ICON_FEMALE, "Mother of: " + obj.toString())));
    return true;/*  w ww.java 2s .  c o m*/
}

From source file:net.landora.animeinfo.notifications.NotificationViewer.java

private void addChildren(DefaultMutableTreeNode node, List<Object> items) {
    if (node.getAllowsChildren()) {
        for (int i = 0; i < node.getChildCount(); i++) {
            DefaultMutableTreeNode n = (DefaultMutableTreeNode) node.getChildAt(i);
            addChildren(n, items);/*www  . jav  a2  s  .  co m*/
        }
    } else {
        items.add(node.getUserObject());
    }
}

From source file:Main.java

public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) {
    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
    if (parent == null) {
        parent = rootNode;//from  ww w.j  av  a 2 s  .  co  m
    }
    treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
    if (shouldBeVisible) {
        tree.scrollPathToVisible(new TreePath(childNode.getPath()));
    }
    return childNode;
}

From source file:TreeUtil.java

/** Creates the menus by using recursion */
public JMenuItem getMenus(DefaultMutableTreeNode node, JMenu parentMenu) {
    String name = node.getUserObject().toString();
    int numChild = node.getChildCount();
    if (numChild < 1) {
        JMenuItem tempMenu = new JMenuItem(name);
        tempMenu.setActionCommand(parentMenu.getActionCommand() + "." + name);
        tempMenu.addActionListener(this);
        return tempMenu;
    }//from w  ww  . j  av  a 2s . c o  m
    JMenu tempMenu = new JMenu(name);
    tempMenu.setActionCommand(parentMenu.getActionCommand() + "." + name);
    tempMenu.addActionListener(this);
    for (int i = 0; i < numChild; i++) {
        tempMenu.add(getMenus((DefaultMutableTreeNode) node.getChildAt(i), tempMenu));
    }
    return tempMenu;
}

From source file:uk.co.markfrimston.tasktree.Main.java

protected void promptAndRemove(DefaultMutableTreeNode node) {
    boolean canRemove = true;
    if (node.getChildCount() > 0) {
        int resp = JOptionPane.showConfirmDialog(this, "Item contains nested items. Remove anyway?");
        if (resp != JOptionPane.YES_OPTION) {
            canRemove = false;//w w w  .  ja va2s  .co m
        }
    }
    if (canRemove) {
        this.taskTree.removeTask(node);
    }
}