Example usage for javax.swing.tree DefaultMutableTreeNode add

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

Introduction

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

Prototype

public void add(MutableTreeNode newChild) 

Source Link

Document

Removes newChild from its parent and makes it a child of this node by adding it to the end of this node's child array.

Usage

From source file:SwingDnDTest.java

public static JTree tree() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("World");
    DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA");
    root.add(country);
    DefaultMutableTreeNode state = new DefaultMutableTreeNode("California");
    country.add(state);/*from www.ja v a2  s  .  c o m*/
    DefaultMutableTreeNode city = new DefaultMutableTreeNode("San Jose");
    state.add(city);
    city = new DefaultMutableTreeNode("Cupertino");
    state.add(city);
    state = new DefaultMutableTreeNode("Michigan");
    country.add(state);
    city = new DefaultMutableTreeNode("Ann Arbor");
    state.add(city);
    country = new DefaultMutableTreeNode("Germany");
    root.add(country);
    state = new DefaultMutableTreeNode("Schleswig-Holstein");
    country.add(state);
    city = new DefaultMutableTreeNode("Kiel");
    state.add(city);
    return new JTree(root);
}

From source file:Main.java

private static void fillTree(DefaultMutableTreeNode parent, int level, String label) {
    for (int i = 0; i < 5; i++) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(label + " " + i);
        parent.add(node);
        if (level > 0) {
            fillTree(node, level - 1, label);
        }/*  w  w w .j a  va  2 s  .com*/
    }
}

From source file:Main.java

private static TreeNode getNodes(DefaultMutableTreeNode parent, int i) {
    if (i > 0) {
        for (int j = 0; j < 5; j++) {
            DefaultMutableTreeNode newChild = new DefaultMutableTreeNode("<html>Node " + (j + 1)
                    + " <a href=\"http://www.java2s.com\">java2s.com</a></html> and text");
            getNodes(newChild, i - 1);//from ww  w.  j  a v a 2 s  . c o  m
            parent.add(newChild);
        }
    }
    return parent;
}

From source file:LocationSensitiveDemo.java

private static DefaultTreeModel getDefaultTreeModel() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("things");
    DefaultMutableTreeNode parent;
    DefaultMutableTreeNode nparent;

    parent = new DefaultMutableTreeNode("colors");
    root.add(parent);
    parent.add(new DefaultMutableTreeNode("red"));
    parent.add(new DefaultMutableTreeNode("yellow"));
    parent.add(new DefaultMutableTreeNode("green"));
    parent.add(new DefaultMutableTreeNode("blue"));
    parent.add(new DefaultMutableTreeNode("purple"));

    parent = new DefaultMutableTreeNode("names");
    root.add(parent);//from  w  ww .  j av  a2s. co  m
    nparent = new DefaultMutableTreeNode("men");
    nparent.add(new DefaultMutableTreeNode("jack"));
    nparent.add(new DefaultMutableTreeNode("kieran"));
    nparent.add(new DefaultMutableTreeNode("william"));
    nparent.add(new DefaultMutableTreeNode("jose"));

    parent.add(nparent);
    nparent = new DefaultMutableTreeNode("women");
    nparent.add(new DefaultMutableTreeNode("jennifer"));
    nparent.add(new DefaultMutableTreeNode("holly"));
    nparent.add(new DefaultMutableTreeNode("danielle"));
    nparent.add(new DefaultMutableTreeNode("tara"));
    parent.add(nparent);

    parent = new DefaultMutableTreeNode("sports");
    root.add(parent);
    parent.add(new DefaultMutableTreeNode("basketball"));
    parent.add(new DefaultMutableTreeNode("soccer"));
    parent.add(new DefaultMutableTreeNode("football"));

    nparent = new DefaultMutableTreeNode("hockey");
    parent.add(nparent);
    nparent.add(new DefaultMutableTreeNode("ice hockey"));
    nparent.add(new DefaultMutableTreeNode("roller hockey"));
    nparent.add(new DefaultMutableTreeNode("floor hockey"));
    nparent.add(new DefaultMutableTreeNode("road hockey"));

    parent = new DefaultMutableTreeNode("food");
    root.add(parent);
    parent.add(new DefaultMutableTreeNode("pizza"));
    parent.add(new DefaultMutableTreeNode("wings"));
    parent.add(new DefaultMutableTreeNode("pasta"));
    nparent = new DefaultMutableTreeNode("fruit");
    parent.add(nparent);
    nparent.add(new DefaultMutableTreeNode("bananas"));
    nparent.add(new DefaultMutableTreeNode("apples"));
    nparent.add(new DefaultMutableTreeNode("grapes"));
    nparent.add(new DefaultMutableTreeNode("pears"));
    return new DefaultTreeModel(root);
}

From source file:com.imaginea.betterdocs.BetterDocsAction.java

private static DefaultMutableTreeNode updateRoot(DefaultMutableTreeNode root,
        Map<String, ArrayList<CodeInfo>> projectNodes) {
    for (Map.Entry<String, ArrayList<CodeInfo>> entry : projectNodes.entrySet()) {
        root.add(getNodes(entry.getKey(), entry.getValue()));
    }/*from  w w w. j a  va 2 s  . c o  m*/
    return root;
}

From source file:ws.moor.bt.grapher.Grapher.java

private static TreeNode convertToTree(List<String> names) {
    Map<String, DefaultMutableTreeNode> existingNodes = new HashMap<String, DefaultMutableTreeNode>();

    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Counters");

    for (String name : names) {
        DefaultMutableTreeNode bestFittingParent = rootNode;
        String[] parts = name.split("\\.|@");
        StringBuilder prefix = new StringBuilder();
        for (int i = 0; i < parts.length - 1; i++) {
            prefix.append(parts[i]);//from w  w w. j a v a 2 s.  c  o m
            DefaultMutableTreeNode parent = existingNodes.get(prefix.toString());
            if (parent != null) {
                bestFittingParent = parent;
            } else {
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(parts[i]);
                bestFittingParent.add(newNode);
                existingNodes.put(prefix.toString(), newNode);
                bestFittingParent = newNode;
            }
        }

        DefaultMutableTreeNode currentNode = new DefaultMutableTreeNode(new NodeValue(name));
        bestFittingParent.add(currentNode);
        currentNode.setAllowsChildren(false);
    }
    return rootNode;
}

From source file:com.imaginea.betterdocs.BetterDocsAction.java

private static MutableTreeNode getNodes(String projectName, Iterable<CodeInfo> codeInfoCollection) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(projectName);
    Collection<String> fileNameSet = new HashSet<String>();
    for (CodeInfo codeInfo : codeInfoCollection) {
        if (!fileNameSet.contains(codeInfo.getFileName())) {
            node.add(new DefaultMutableTreeNode(codeInfo));
            fileNameSet.add(codeInfo.getFileName());
        }//ww w.jav  a 2  s  .c o m
    }
    return node;
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("abcde");
    DefaultMutableTreeNode node = new DefaultMutableTreeNode("1");
    node.add(new DefaultMutableTreeNode("12345"));
    node.add(new DefaultMutableTreeNode("testing"));
    root.add(node);//from w w w . j  ava2s  . c  o  m
    root.add(new DefaultMutableTreeNode("1234567890"));

    TreeModel tm = new DefaultTreeModel(root);
    JTree tree = new JTree(tm);
    tree.getSelectionModel().addTreeSelectionListener(new Selector());
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    getContentPane().add(tree, BorderLayout.CENTER);
    pack();
    setVisible(true);
}

From source file:TreeCellRendererImplementation.java

public TreeCellRendererImplementation() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("+");
    root.add(new DefaultMutableTreeNode(new Integer(3)));
    DefaultMutableTreeNode node = new DefaultMutableTreeNode("*");
    node.add(new DefaultMutableTreeNode("string"));
    node.add(new DefaultMutableTreeNode(new Short((short) 5)));
    root.add(node);// w  w  w  .j  av a2 s. c om

    TreeModel tm = new DefaultTreeModel(root);
    JTree tree = new JTree(tm);
    tree.setShowsRootHandles(true);
    tree.setCellRenderer(new MyRenderer());
    getContentPane().add(tree, BorderLayout.CENTER);
    setSize(400, 300);
    setVisible(true);
}

From source file:Main.java

public Main() {
    DefaultMutableTreeNode AA1 = new DefaultMutableTreeNode("AA1");
    DefaultMutableTreeNode AA2 = new DefaultMutableTreeNode("AA2");
    DefaultMutableTreeNode A = new DefaultMutableTreeNode("A");
    A.add(AA1);
    A.add(AA2);// w w  w. ja  va 2s.  c o m
    DefaultMutableTreeNode BB1 = new DefaultMutableTreeNode("BB1");
    DefaultMutableTreeNode BB2 = new DefaultMutableTreeNode("BB2");
    DefaultMutableTreeNode B = new DefaultMutableTreeNode("B");
    B.add(BB1);
    B.add(BB2);
    DefaultMutableTreeNode CC1 = new DefaultMutableTreeNode("CC1");
    DefaultMutableTreeNode CC2 = new DefaultMutableTreeNode("CC2");
    DefaultMutableTreeNode C = new DefaultMutableTreeNode("C");
    C.add(CC1);
    C.add(CC2);
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    root.add(A);
    root.add(B);
    root.add(C);
    tree = new JTree(root);
    tree.setCellRenderer(new MyTableInTreeCellRenderer());
    tree.setRowHeight(0);
    JScrollPane jsp = new JScrollPane(tree);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(jsp, BorderLayout.CENTER);
    pack();
}