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 static void main(String[] argv) throws Exception {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root Label");
    root.add(new DefaultMutableTreeNode("Node Label"));

    JTree tree = new JTree(root);

    JFrame f = new JFrame();
    f.add(new JScrollPane(tree));
    f.setSize(300, 300);//  ww w. j  a v a2  s  . c  om
    f.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame(Main.class.getSimpleName());
    DefaultTreeModel model = getTreeModel();
    JTree tree1 = new JTree(model);
    JTree tree2 = new JTree(model);
    frame.add(new JScrollPane(tree1), BorderLayout.WEST);
    frame.add(new JScrollPane(tree2), BorderLayout.EAST);
    frame.pack();/*from  w w  w  .  j  av  a  2s.c o m*/
    frame.setSize(frame.getWidth() + 50, frame.getHeight() + 140);
    frame.setVisible(true);
    Timer t = new Timer(2000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
            root.add(new DefaultMutableTreeNode("A new node"));
            model.nodesWereInserted(root, new int[] { root.getChildCount() - 1 });
            tree1.expandRow(0);
            tree2.expandRow(0);
            frame.revalidate();
        }
    });
    t.start();
}

From source file:TreeExpansionListenerDemo.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    root.add(new DefaultMutableTreeNode("A"));
    root.add(new DefaultMutableTreeNode("B"));
    root.add(new DefaultMutableTreeNode("C"));
    JTree tree = new JTree(root);

    TreeExpansionListener treeExpandListener = new TreeExpansionListener() {

        public void treeExpanded(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            System.out.println("Expanded: " + data);
        }// w  w w. j a  v  a2  s.com

        public void treeCollapsed(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            System.out.println("Collapsed: " + data);
        }
    };

    tree.addTreeExpansionListener(treeExpandListener);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    root.add(new DefaultMutableTreeNode("A"));
    root.add(new DefaultMutableTreeNode("B"));
    root.add(new DefaultMutableTreeNode("C"));
    JTree tree = new JTree(root);

    TreeWillExpandListener treeWillExpandListener = new TreeWillExpandListener() {
        public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {
            TreePath path = treeExpansionEvent.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            System.out.println("WillCollapse: " + data);

        }//  w  ww.  j a v  a  2  s.  c  o  m

        public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {
            TreePath path = treeExpansionEvent.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            System.out.println("WillExpand: " + data);

        }
    };

    tree.addTreeWillExpandListener(treeWillExpandListener);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);

}

From source file:DefaultMutableTreeNodeAddingDemo.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode mercury = new DefaultMutableTreeNode("Mercury");
    root.add(mercury);
    DefaultMutableTreeNode venus = new DefaultMutableTreeNode("Venus");
    root.add(venus);//from   w ww .j  a  v a 2  s  .com
    DefaultMutableTreeNode mars = new DefaultMutableTreeNode("Mars");
    root.add(mars);
    JTree tree = new JTree(root);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:PrintDescendants.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode mercury = new DefaultMutableTreeNode("Mercury");
    root.add(mercury);
    DefaultMutableTreeNode venus = new DefaultMutableTreeNode("Venus");
    root.add(venus);//from  w w  w .j  a va  2s.  co m
    DefaultMutableTreeNode mars = new DefaultMutableTreeNode("Mars");
    root.add(mars);
    JTree tree = new JTree(root);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);

    printDescendants(root);
}

From source file:Main.java

public static void main(String[] args) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Days");
    for (DaysOfTheWeek dotw : DaysOfTheWeek.values()) {
        root.add(new DefaultMutableTreeNode(dotw));
    }// w  ww  .  j av  a 2  s . com
    final DefaultTreeModel model = new DefaultTreeModel(root);
    JTree tree = new JTree(model);
    tree.setRootVisible(true);
    tree.setShowsRootHandles(true);
    ToolTipManager.sharedInstance().registerComponent(tree);
    tree.setCellRenderer(new MyTreeCellRenderer());
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPane(tree));
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String[] m = { "A", "B", "C" };
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(new Node("Values"));
    root.add(new DefaultMutableTreeNode(new Node("Value 1", m)));
    root.add(new DefaultMutableTreeNode(new Node("Value 2", m)));
    DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(new Node("Value 3", m));
    root.add(leaf);/*  ww  w  .  ja va2s  .  com*/
    leaf.add(new DefaultMutableTreeNode(new Node("Value 3A", m)));
    leaf.add(new DefaultMutableTreeNode(new Node("Value 3B", m)));

    JTree tree = new JTree(root);
    RendererDispatcher rendererDispatcher = new RendererDispatcher(new JComboBox<String>());
    RendererDispatcher editorDispatcher = new RendererDispatcher(new JComboBox<String>());
    tree.setCellRenderer(rendererDispatcher);
    tree.setCellEditor(editorDispatcher);
    tree.setEditable(true);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(tree));
    f.setSize(320, 240);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("JTree Demo");
    f.setSize(260, 240);//from   ww w . j av a2  s . co m
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode aNode = new DefaultMutableTreeNode("A");
    root.add(aNode);

    DefaultMutableTreeNode bNode = new DefaultMutableTreeNode("B");
    aNode.add(bNode);

    DefaultMutableTreeNode cNode = new DefaultMutableTreeNode("C");
    aNode.add(cNode);

    cNode.add(new DefaultMutableTreeNode("D"));
    cNode.add(new DefaultMutableTreeNode("E"));

    DefaultMutableTreeNode fNode = new DefaultMutableTreeNode("F");
    root.add(fNode);

    DefaultMutableTreeNode gNode = new DefaultMutableTreeNode("G");
    fNode.add(gNode);
    fNode.add(new DefaultMutableTreeNode("H"));
    gNode.add(new DefaultMutableTreeNode("I"));

    JTree jtree = new JTree(root);
    jtree.setEditable(true);

    TreeSelectionModel tsm = jtree.getSelectionModel();
    tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    jtree.addTreeExpansionListener(new TreeExpansionListener() {
        public void treeExpanded(TreeExpansionEvent tee) {
            TreePath tp = tee.getPath();
            System.out.println("Expansion: " + tp.getLastPathComponent());
        }

        public void treeCollapsed(TreeExpansionEvent tee) {
            TreePath tp = tee.getPath();
            System.out.println("Collapse: " + tp.getLastPathComponent());
        }
    });

    jtree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent tse) {
            TreePath tp = tse.getPath();
            System.out.println("Selection event: " + tp.getLastPathComponent());
        }
    });

    jtree.getModel().addTreeModelListener(new TreeModelListener() {
        public void treeNodesChanged(TreeModelEvent tme) {
            TreePath tp = tme.getTreePath();
            Object[] children = tme.getChildren();
            DefaultMutableTreeNode changedNode;
            if (children != null)
                changedNode = (DefaultMutableTreeNode) children[0];
            else
                changedNode = (DefaultMutableTreeNode) tp.getLastPathComponent();

            System.out.println("Model change path: " + tp + "New data: " + changedNode.getUserObject());

        }

        public void treeNodesInserted(TreeModelEvent tme) {
        }

        public void treeNodesRemoved(TreeModelEvent tme) {
        }

        public void treeStructureChanged(TreeModelEvent tme) {
        }
    });

    f.add(new JScrollPane(jtree));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(Boolean.TRUE);
    DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(Boolean.FALSE);
    DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(Boolean.FALSE);
    root.add(child1);
    root.add(child2);//ww  w . ja  va  2s  . c  o  m
    DefaultTreeModel model = new DefaultTreeModel(root);

    JTree tree = new JTree(model);
    tree.setCellRenderer(new TreeRenderer());

    tree.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            TreePath pathForLocation = tree.getPathForLocation(e.getX(), e.getY());
            Object lastPathComponent = pathForLocation.getLastPathComponent();
            if (lastPathComponent instanceof DefaultMutableTreeNode) {
                Boolean oldObject = (Boolean) ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
                ((DefaultMutableTreeNode) lastPathComponent).setUserObject(!oldObject);
                model.nodeChanged((DefaultMutableTreeNode) lastPathComponent);
            }
        }
    });

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(tree, BorderLayout.CENTER);
    frame.setSize(800, 600);
    frame.setVisible(true);
}