Example usage for javax.swing JTree JTree

List of usage examples for javax.swing JTree JTree

Introduction

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

Prototype

@ConstructorProperties({ "model" })
public JTree(TreeModel newModel) 

Source Link

Document

Returns an instance of JTree which displays the root node -- the tree is created using the specified data model.

Usage

From source file:Main.java

public Main() {
    Object[] genealogy = { "A", "B", "C", "D", "E", "F" };
    java.util.Vector v = new java.util.Vector() {
        public String toString() {
            return "DD";
        }//from   ww  w  .  j  a  v  a  2s .c om
    };
    v.addElement("1");
    v.addElement("2");
    v.addElement("3");
    v.addElement("4");
    genealogy[0] = v;
    JTree tree = new JTree(genealogy);
    tree.setRootVisible(true);
    JScrollPane jsp = new JScrollPane(tree);
    getContentPane().add(jsp);
}

From source file:SimpleTreeTest.java

public SimpleTreeTest() {
    Object[] genealogy = { "A", "B", "C", "D", "E", "F" };
    java.util.Vector v = new java.util.Vector() {
        public String toString() {
            return "DD";
        }/*from  ww  w.ja  v  a  2  s . c  o m*/
    };
    v.addElement("1");
    v.addElement("2");
    v.addElement("3");
    v.addElement("4");
    genealogy[0] = v;
    JTree tree = new JTree(genealogy);
    tree.setRootVisible(true);
    JScrollPane jsp = new JScrollPane(tree);
    getContentPane().add(jsp);
}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);/*www  . j ava2s  .  co  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: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));
    }/*from w  w  w .  j a  va  2  s  .c  o  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(File dir) {
    setLayout(new BorderLayout());
    JTree tree = new JTree(addNodes(null, projectFile));
    tree.setCellRenderer(new MyTreeCellRenderer());
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
            System.out.println("You selected " + node);
        }//from  ww w  . j  a v a  2 s . c o m
    });

    JScrollPane scrollpane = new JScrollPane();
    scrollpane.getViewport().add(tree);
    add(scrollpane, BorderLayout.CENTER);
}

From source file:MainClass.java

public MainClass(String startDir) {
    super("SortTreeModel Demonstration");
    setSize(300, 400);/*w  w  w .  java  2 s . c o m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    MyFile f = new MyFile(startDir);
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(f);
    SortTreeModel model = new SortTreeModel(root, new TreeStringComparator());
    fillModel(model, root);

    JTree tree = new JTree(model);
    getContentPane().add(new JScrollPane(tree));
}

From source file:Main.java

public FileTree(File dir) {
    setLayout(new BorderLayout());
    JTree tree = new JTree(addNodes(null, dir));
    tree.addTreeSelectionListener(e -> {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
        System.out.println("You selected " + node);
    });//w ww  .j a v  a 2 s  .  co  m
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    JScrollPane scrollpane = new JScrollPane();
    scrollpane.getViewport().add(tree);
    add(BorderLayout.CENTER, scrollpane);
}

From source file:Main.java

public DriveTree(File dir) {
    setLayout(new BorderLayout());
    JTree tree = new JTree(addNodes(null, dir));
    add(tree);
}

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  ww .j  a v  a2 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:MainClass.java

public void init() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
    DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
    DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");

    treeModel = new DefaultTreeModel(root);
    tree = new JTree(treeModel);

    treeModel.insertNodeInto(subroot, root, 0);
    subroot.add(leaf1);/*  ww  w  .  j a v  a 2 s.c o m*/
    root.add(leaf2);

    tree.putClientProperty("JTree.lineStyle", "Angled");
    getContentPane().add(tree, BorderLayout.CENTER);

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    renderer.setClosedIcon(new ImageIcon("door.closed.gif"));
    renderer.setOpenIcon(new ImageIcon("door.open.gif"));
    renderer.setLeafIcon(new ImageIcon("world.gif"));

}