Example usage for javax.swing.tree DefaultTreeModel DefaultTreeModel

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

Introduction

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

Prototype

@ConstructorProperties({ "root" })
public DefaultTreeModel(TreeNode root) 

Source Link

Document

Creates a tree in which any node can have children.

Usage

From source file:Main.java

private static DefaultTreeModel getTreeModel() {
    return new DefaultTreeModel(new DefaultMutableTreeNode("Root object"));
}

From source file:Main.java

private static DefaultTreeModel buildDemoModel() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

    root.add(new DefaultMutableTreeNode("A"));
    root.add(new DefaultMutableTreeNode("B"));
    root.add(new DefaultMutableTreeNode("C"));

    return new DefaultTreeModel(root);
}

From source file:Main.java

private static TreeModel getTreeModel() {
    return new DefaultTreeModel(getNodes(
            new DefaultMutableTreeNode("<html>Root <a href=\"http://www.google.be\">Google</a></html>"), 5));
}

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   w  w  w. jav a2  s. c  o m*/
    f.setSize(300, 300);
    f.setLocation(200, 200);
    f.setVisible(true);
}

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  w  w .  j a  v  a  2s .c o m*/

    this.tree.setModel(model);

    this.pack();
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TreeNode root = getNodes();/*from  w  ww . j  av 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:Main.java

public Main() {
    super(new BorderLayout());
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);/*from  ww  w. j av  a2  s.com*/

    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:Main.java

public Main() {
    setLayout(new BorderLayout());
    treeModel = new DefaultTreeModel(root);
    tree = new JTree(treeModel);
    treeScroll = new JScrollPane(tree);
    add(treeScroll, BorderLayout.WEST);

    properties.setProperty("foo1", "bar1");
    properties.setProperty("foo2", "bar2");
    properties.setProperty("foo3", "bar3");
    properties.setProperty("foo4", "bar4");

    Set<Object> keySet = properties.keySet();
    for (Object key : keySet) {
        root.add(new DefaultMutableTreeNode(key));
    }//www  .j  a v  a  2 s.  co m

    tree.expandPath(new TreePath(root));
    descriptionLabel = new JLabel(NOTHING_SELECTED);
    add(descriptionLabel, BorderLayout.CENTER);

    tree.addTreeSelectionListener(e -> {
        DefaultMutableTreeNode selection = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (selection != null) {
            String key = (String) selection.getUserObject();
            String command = properties.getProperty(key);
            descriptionLabel.setText(command);
        } else {
            descriptionLabel.setText(NOTHING_SELECTED);
        }
    });
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
}

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 ww.  j  ava2s .  co 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);/*from   w  w  w  .  j  ava 2s. co m*/

    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);
}