Example usage for javax.swing.tree DefaultMutableTreeNode DefaultMutableTreeNode

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

Introduction

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

Prototype

public DefaultMutableTreeNode(Object userObject) 

Source Link

Document

Creates a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.

Usage

From source file:Main.java

public static DefaultMutableTreeNode addNodes(File dir) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(dir);
    for (File file : dir.listFiles()) {
        if (file.isDirectory()) {
            node.add(addNodes(file));/*w  w  w.j  a  va2 s .  c  o  m*/
        } else {
            node.add(new DefaultMutableTreeNode(file));
        }
    }
    return node;
}

From source file:Main.java

public Main() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("ROOT");
    DefaultTreeModel model = new DefaultTreeModel(root);
    JTree tree = new JTree(model);

    buildTreeFromString(model, "A/D/E/Node 4");
    buildTreeFromString(model, "A/C/F/Node 5");
    buildTreeFromString(model, "A/B/G/Node 6");
    buildTreeFromString(model, "A/C/H/Node 5");
    buildTreeFromString(model, "A/B/G/Node 5");

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(tree);/*from ww w  .j  av a 2 s.  c  o  m*/
    f.setSize(300, 300);
    f.setLocation(200, 200);
    f.setVisible(true);
}

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);// www  . j  a va 2s.c  om
            parent.add(newChild);
        }
    }
    return parent;
}

From source file:Main.java

private static DefaultMutableTreeNode addCloneNode(DefaultMutableTreeNode srcNode,
        DefaultMutableTreeNode root) {
    DefaultMutableTreeNode clone = new DefaultMutableTreeNode(srcNode.getUserObject());
    root.add(clone);//from ww  w.j a  v  a2 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:Main.java

public Main() {
    super(BoxLayout.Y_AXIS);
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    for (int i = 0; i < 14; i++) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode("Root" + i);
        node.add(new DefaultMutableTreeNode("Child" + i));
        root.add(node);/*from  ww  w  . j  a  v  a2s .  co m*/
    }
    CustomTree tree = new CustomTree(root);
    tree.setRootVisible(false);
    JScrollPane pane = new JScrollPane(tree);
    add(pane);
    JButton button = new JButton("Expand");
    button.addActionListener(e -> tree.expandSelectedPaths());
    add(button);
}

From source file:Main.java

public Main() {
    this.tree = new JTree();
    this.add(this.tree);

    File fileRoot = new File("C:/");

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(fileRoot);
    DefaultTreeModel model = new DefaultTreeModel(root);

    File[] subItems = fileRoot.listFiles();
    for (File file : subItems) {
        root.add(new DefaultMutableTreeNode(file));
    }/*from   w  ww . jav a 2s.  c om*/

    this.tree.setModel(model);

    this.pack();
}

From source file:Main.java

public Main() {
    DefaultMutableTreeNode contacts = new DefaultMutableTreeNode("Contacts");
    createNodes(contacts);/* ww  w .j  a v  a2  s  .  c  o  m*/
    tree = new JTree(contacts);
    tree.setCellRenderer(new MyTreeCellRenderer());
    JScrollPane treeView = new JScrollPane(tree);
    add(treeView);
    setSize(400, 400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

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);// w w w .  j  a va  2 s. 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:Main.java

public Main() {
    DefaultMutableTreeNode AA1 = new DefaultMutableTreeNode("AA1");
    DefaultMutableTreeNode AA2 = new DefaultMutableTreeNode("AA2");
    DefaultMutableTreeNode A = new DefaultMutableTreeNode("A");
    A.add(AA1);//www .  j a va 2s .  c om
    A.add(AA2);
    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();
}

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);/*from  w w w  .j a  v  a 2s  .  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);
}