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: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  www  . ja va  2  s.c  o 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:MainClass.java

private DefaultMutableTreeNode makeShow(String title, DefaultMutableTreeNode parent) {
    DefaultMutableTreeNode show;/*from   w  ww .  j  a  va 2 s  .c o  m*/
    show = new DefaultMutableTreeNode(title);
    parent.add(show);
    return show;
}

From source file:Main.java

public Main() {
    super(BoxLayout.Y_AXIS);
    glassPane.setOpaque(false);//w w w  .j a v  a  2 s.  c om
    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

private void construct(MyObject obj1, DefaultMutableTreeNode root) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(obj1);
    root.add(node);
    for (MyObject o : obj1.childs) {
        construct(o, node);// w w  w .j  av  a2  s.c o m
    }
}

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);
    DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
    a.add(a1);//  w w w .  j av a  2 s  .  c  om
    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:Main.java

public Main() {
    DefaultMutableTreeNode forums = new DefaultMutableTreeNode("B");
    forums.add(new DefaultMutableTreeNode("T"));
    DefaultMutableTreeNode articles = new DefaultMutableTreeNode("A");
    articles.add(new DefaultMutableTreeNode("A"));
    DefaultMutableTreeNode examples = new DefaultMutableTreeNode("E");
    examples.add(new DefaultMutableTreeNode("E"));

    rootNode.add(forums);//from w  w w  .  j ava 2s .  c  o m
    rootNode.add(articles);
    rootNode.add(examples);

    m_tree.setEditable(true);
    m_tree.setSelectionRow(0);

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

    JPanel panel = new JPanel();
    m_addButton = new JButton("Add Node");
    m_addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();

            if (selNode == null) {
                return;
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New");
            m_model.insertNodeInto(newNode, selNode, selNode.getChildCount());

            TreeNode[] nodes = m_model.getPathToRoot(newNode);
            TreePath path = new TreePath(nodes);
            m_tree.scrollPathToVisible(path);

            m_tree.setSelectionPath(path);

            m_tree.startEditingAtPath(path);
        }

    });
    panel.add(m_addButton);
    getContentPane().add(panel, BorderLayout.SOUTH);
    m_delButton = new JButton("Delete Node");
    m_delButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();
            if (selNode == null) {
                return;
            }
            MutableTreeNode parent = (MutableTreeNode) (selNode.getParent());
            if (parent == null) {
                return;
            }
            MutableTreeNode toBeSelNode = (MutableTreeNode) selNode.getPreviousSibling();
            if (toBeSelNode == null) {
                toBeSelNode = (MutableTreeNode) selNode.getNextSibling();
            }
            if (toBeSelNode == null) {
                toBeSelNode = parent;
            }
            TreeNode[] nodes = m_model.getPathToRoot(toBeSelNode);
            TreePath path = new TreePath(nodes);
            m_tree.scrollPathToVisible(path);
            m_tree.setSelectionPath(path);
            m_model.removeNodeFromParent(selNode);
        }

    });
    panel.add(m_delButton);
    getContentPane().add(panel, BorderLayout.SOUTH);

    setSize(300, 400);
    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));
    }/* w  w  w . j a  va 2  s.  c  o m*/

    this.tree.setModel(model);

    this.pack();
}

From source file:Main.java

public void load(boolean isAlpha) {
    rootNode.removeAllChildren();//w w  w  .  ja v  a2  s  . com

    String[] values;
    if (isAlpha)
        values = new String[] { "A", "A1", "A2", "B", "B1", "B2" };
    else
        values = new String[] { "10", "11", "12", "20", "21", "22" };

    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(values[0]);
    DefaultMutableTreeNode leafNode = new DefaultMutableTreeNode(values[1]);
    childNode.add(leafNode);
    leafNode = new DefaultMutableTreeNode(values[2]);
    childNode.add(leafNode);

    rootNode.add(childNode);

    childNode = new DefaultMutableTreeNode(values[3]);
    leafNode = new DefaultMutableTreeNode(values[4]);
    childNode.add(leafNode);
    leafNode = new DefaultMutableTreeNode(values[5]);
    childNode.add(leafNode);
    rootNode.add(childNode);

    nodeStructureChanged(rootNode);
}

From source file:TreeIt.java

public TreeIt() {
    JFrame f = new JFrame();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar");
    DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months");
    root.add(months);
    String monthLabels[] = { "January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December" };
    for (int i = 0, n = monthLabels.length; i < n; i++)
        months.add(new DefaultMutableTreeNode(monthLabels[i]));
    DefaultMutableTreeNode weeks = new DefaultMutableTreeNode("Weeks");
    root.add(weeks);/*from  w w w .  j a  va  2 s  . co m*/
    String weekLabels[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    for (int i = 0, n = weekLabels.length; i < n; i++)
        weeks.add(new DefaultMutableTreeNode(weekLabels[i]));
    JTree jt = new JTree(root);
    jt.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            TreePath path = e.getPath();
            System.out.println("Picked: " + path.getLastPathComponent());
            Object elements[] = path.getPath();
            for (int i = 0, n = elements.length; i < n; i++) {
                System.out.print("->" + elements[i]);
            }
            System.out.println();
        }
    });

    DefaultMutableTreeNode lastLeaf = root.getLastLeaf();
    TreePath path = new TreePath(lastLeaf.getPath());
    jt.setSelectionPath(path);

    jt.setCellRenderer(new MyCellRenderer());

    JScrollPane jsp = new JScrollPane(jt);
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.CENTER);
    f.setSize(250, 250);
    f.show();
}

From source file:StAXStreamTreeViewer.java

private void addStartDocumentNodes(XMLStreamReader reader, DefaultMutableTreeNode current) {
    DefaultMutableTreeNode version = new DefaultMutableTreeNode(reader.getVersion());
    current.add(version);

    DefaultMutableTreeNode standalone = new DefaultMutableTreeNode(reader.isStandalone());
    current.add(standalone);/*w w w  .  jav  a 2  s  .c  om*/

    DefaultMutableTreeNode standaloneSet = new DefaultMutableTreeNode(reader.standaloneSet());
    current.add(standaloneSet);

    DefaultMutableTreeNode encoding = new DefaultMutableTreeNode(reader.getEncoding());
    current.add(encoding);

    DefaultMutableTreeNode declaredEncoding = new DefaultMutableTreeNode(reader.getCharacterEncodingScheme());
    current.add(declaredEncoding);
}