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: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);//from   w ww.ja v a 2  s  .  c  o  m
    root.add(leaf2);

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

From source file:MainClass.java

public MainClass() {
    final JTree tree;
    final JTextField jtf;

    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");

    DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
    top.add(a);/*from  w  w  w.  j  av a2 s.  com*/
    DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
    a.add(a1);
    DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
    a.add(a2);

    DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
    top.add(b);
    DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
    b.add(b1);
    DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
    b.add(b2);
    DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
    b.add(b3);

    tree = new JTree(top);

    JScrollPane jsp = new JScrollPane(tree);

    add(jsp, BorderLayout.CENTER);

    jtf = new JTextField("", 20);
    add(jtf, BorderLayout.SOUTH);

    tree.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
            if (tp != null)
                jtf.setText(tp.toString());
            else
                jtf.setText("");
        }
    });

}

From source file:MyData.java

public Main() {
    super();/*from w  w w.  j  a v a 2  s. c om*/
    JTree tree = new JTree(getRootNode()) {
        public boolean isPathEditable(TreePath path) {
            Object comp = path.getLastPathComponent();
            if (comp instanceof DefaultMutableTreeNode) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) comp;
                Object userObject = node.getUserObject();
                if (userObject instanceof Main) {
                    return true;
                }
            }
            return false;
        }
    };
    QuestionCellRenderer renderer = new QuestionCellRenderer();
    tree.setCellRenderer(renderer);
    QuestionCellEditor editor = new QuestionCellEditor();
    tree.setCellEditor(editor);
    tree.setEditable(true);
    JScrollPane jsp = new JScrollPane(tree);
    getContentPane().add(jsp);
}

From source file:Main.java

public Main() {
    super(BoxLayout.Y_AXIS);
    glassPane.setOpaque(false);/*from  ww w .  ja v a2 s .  c  o m*/
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    for (int i = 0; i < 14000; i++) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode("Root" + i);
        node.add(new DefaultMutableTreeNode("Child" + i));
        root.add(node);
    }

    final JTree tree = new JTree(root);
    tree.setRootVisible(false);
    final JScrollPane pane = new JScrollPane(tree);
    add(pane);

    JButton button = new JButton("Expand");
    button.addActionListener(e -> {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                TreePath[] paths = tree.getSelectionPaths();
                if (paths == null || paths.length == 0) {
                    glassPane.setVisible(false);
                    return;
                }
                tree.setSelectionPath(paths[0]);

                for (int i = 0; i < paths.length; i++) {
                    tree.expandPath(paths[i]);
                }
                glassPane.setVisible(false);
            }
        });
        getRootPane().setGlassPane(glassPane);
        glassPane.setVisible(true);
        t.start();
    });
    add(button);
    glassPane.addMouseWheelListener(e -> {
        for (MouseWheelListener mwl : pane.getMouseWheelListeners()) {
            mwl.mouseWheelMoved(e);
        }
    });
}

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);//  w  w  w  .  j  a v a2  s.c o m
    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:TestTree.java

public void init() {
    // Build up a bunch of TreeNodes. We use DefaultMutableTreeNode because
    // the//from w  w w .j av a 2  s .co m
    // DefaultTreeModel can use it to build a complete tree.
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
    DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
    DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");

    // Build our tree model starting at the root node, and then make a JTree
    // out
    // of that.
    treeModel = new DefaultTreeModel(root);
    tree = new JTree(treeModel);

    // Build the tree up from the nodes we created.
    treeModel.insertNodeInto(subroot, root, 0);
    // Or, more succinctly:
    subroot.add(leaf1);
    root.add(leaf2);

    // Display it.
    getContentPane().add(tree, BorderLayout.CENTER);
}

From source file:XMLTreeView.java

public XMLTreeView(JFrame frame) {
    frame.getContentPane().setLayout(new BorderLayout());
    DefaultMutableTreeNode top = new DefaultMutableTreeNode(file);
    //              DefaultMutableTreeNode top = new DefaultMutableTreeNode("XML Document"); 

    saxTree = new SAXTreeBuilder(top);

    try {/*w  w w. j  av a 2s .  co  m*/
        SAXParser saxParser = new SAXParser();
        saxParser.setContentHandler(saxTree);
        saxParser.parse(new InputSource(new FileInputStream(file)));
    } catch (Exception ex) {
        top.add(new DefaultMutableTreeNode(ex.getMessage()));
    }
    JTree tree = new JTree(saxTree.getTree());
    JScrollPane scrollPane = new JScrollPane(tree);

    frame.getContentPane().add("Center", scrollPane);
    frame.setVisible(true);

}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    root = new DefaultMutableTreeNode("Root");
    root.add(e1);/*from   w w w.j av a2s . c  om*/
    root.add(e2);
    root.add(e3);
    e1.add(e11);
    e2.add(e22);
    e3.add(e33);
    e11.add(e111);
    e22.add(e222);
    e33.add(e333);

    tree = new JTree(root);
    tree.addTreeSelectionListener(this);
    add(new JScrollPane(tree), BorderLayout.CENTER);
    currentSelectionField = new JTextField("Current Selection: NONE");
    add(currentSelectionField, BorderLayout.SOUTH);
    setSize(250, 275);
    setLocationRelativeTo(null);
    setVisible(true);
}

From source file:Branch.java

public void init() {
    Container cp = getContentPane();
    root = new DefaultMutableTreeNode("root");
    tree = new JTree(root);
    // Add it and make it take care of scrolling:
    cp.add(new JScrollPane(tree), BorderLayout.CENTER);
    // Capture the tree's model:
    model = (DefaultTreeModel) tree.getModel();
    JButton test = new JButton("Press me");
    test.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (i < data.length) {
                child = new Branch(data[i++]).node();
                // What's the last one you clicked?
                chosen = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                if (chosen == null)
                    chosen = root;/*from   ww  w.j a v  a 2  s  . co  m*/
                // The model will create the appropriate event.
                // In response, the tree will update itself:
                model.insertNodeInto(child, chosen, 0);
                // Puts the new node on the chosen node.
            }
        }
    });
    // Change the button's colors:
    test.setBackground(Color.BLUE);
    test.setForeground(Color.WHITE);
    JPanel p = new JPanel();
    p.add(test);
    cp.add(p, BorderLayout.SOUTH);
}

From source file:FileTree2.java

public FileTree2() {
    super("Directories Tree [Popup Menus]");
    setSize(400, 300);/*from   w  ww  .  j  a  v a2 s .c o m*/

    DefaultMutableTreeNode top = new DefaultMutableTreeNode(new IconData(ICON_COMPUTER, null, "Computer"));

    DefaultMutableTreeNode node;
    File[] roots = File.listRoots();
    for (int k = 0; k < roots.length; k++) {
        node = new DefaultMutableTreeNode(new IconData(ICON_DISK, null, new FileNode(roots[k])));
        top.add(node);
        node.add(new DefaultMutableTreeNode(new Boolean(true)));
    }

    m_model = new DefaultTreeModel(top);
    m_tree = new JTree(m_model);

    m_tree.putClientProperty("JTree.lineStyle", "Angled");

    TreeCellRenderer renderer = new IconCellRenderer();
    m_tree.setCellRenderer(renderer);

    m_tree.addTreeExpansionListener(new DirExpansionListener());

    m_tree.addTreeSelectionListener(new DirSelectionListener());

    m_tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    m_tree.setShowsRootHandles(true);
    m_tree.setEditable(false);

    JScrollPane s = new JScrollPane();
    s.getViewport().add(m_tree);
    getContentPane().add(s, BorderLayout.CENTER);

    m_display = new JTextField();
    m_display.setEditable(false);
    getContentPane().add(m_display, BorderLayout.NORTH);

    // NEW
    m_popup = new JPopupMenu();
    m_action = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            if (m_clickedPath == null)
                return;
            if (m_tree.isExpanded(m_clickedPath))
                m_tree.collapsePath(m_clickedPath);
            else
                m_tree.expandPath(m_clickedPath);
        }
    };
    m_popup.add(m_action);
    m_popup.addSeparator();

    Action a1 = new AbstractAction("Delete") {
        public void actionPerformed(ActionEvent e) {
            m_tree.repaint();
            JOptionPane.showMessageDialog(FileTree2.this, "Delete option is not implemented", "Info",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    };
    m_popup.add(a1);

    Action a2 = new AbstractAction("Rename") {
        public void actionPerformed(ActionEvent e) {
            m_tree.repaint();
            JOptionPane.showMessageDialog(FileTree2.this, "Rename option is not implemented", "Info",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    };
    m_popup.add(a2);
    m_tree.add(m_popup);
    m_tree.addMouseListener(new PopupTrigger());

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}