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

public static void main(String[] args) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    DefaultTreeModel model = new DefaultTreeModel(root);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(new JTree(model)));
    f.getContentPane().add(new JButton(new AbstractAction("Add thousand children") {
        @Override//  w w w . j  ava  2s  . c o m
        public void actionPerformed(ActionEvent e) {
            int offset = root.getChildCount() + 1;
            for (int i = 0; i < 1000; i++) {
                DefaultMutableTreeNode child = new DefaultMutableTreeNode("Person " + (i + offset));
                // model.insertNodeInto(child, root, root.getChildCount());
                root.add(child);
            }
            model.nodeStructureChanged(root);
        }
    }), BorderLayout.PAGE_END);
    f.pack();
    f.setVisible(true);
}

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));
    }/*from  w ww  .  j  ava2 s  . co m*/
    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) {

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(Boolean.TRUE);
    DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(Boolean.FALSE);
    DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(Boolean.FALSE);
    root.add(child1);/*from  ww w.j a v a  2 s  . c o m*/
    root.add(child2);
    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);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyTreeNode root = new MyTreeNode(1, 0);
    DefaultTreeModel model = new DefaultTreeModel(root);
    JProgressBar bar = new JProgressBar();
    PropertyChangeListener progressListener = new PropertyChangeListener() {
        @Override/*from  w  w w.java 2  s  .  com*/
        public void propertyChange(PropertyChangeEvent evt) {
            bar.setValue((Integer) evt.getNewValue());
        }
    };
    JTree tree = new JTree();
    tree.setShowsRootHandles(true);
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {
        @Override
        public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
            TreePath path = event.getPath();
            if (path.getLastPathComponent() instanceof MyTreeNode) {
                MyTreeNode node = (MyTreeNode) path.getLastPathComponent();
                node.loadChildren(model, progressListener);
            }
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {

        }
    });
    tree.setModel(model);
    root.loadChildren(model, progressListener);
    frame.add(new JScrollPane(tree));
    frame.add(bar, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    Node root = new Node("Root");
    fillTree(root, 5, "tree label");
    DefaultTreeModel model = new DefaultTreeModel(root);

    JTree tree = new JTree(model);
    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();/*from  w w  w  .ja v a  2 s  .c o  m*/
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    fillTree(root, 5, "Tree label");
    final DefaultTreeModel model = new DefaultTreeModel(root);
    JTree tree = new JTree(model);
    tree.setSelectionModel(new MyTreeSelectionModel());
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPane(tree));
    f.setSize(400, 600);// w w  w . j av a2s . com
    f.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) {
    // create a hierarchy of nodes
    MutableTreeNode root = new DefaultMutableTreeNode("A");
    MutableTreeNode bNode = new DefaultMutableTreeNode("B");
    MutableTreeNode cNode = new DefaultMutableTreeNode("C");
    root.insert(bNode, 0);// w w  w  . j  a  va  2 s .  c  o m
    root.insert(cNode, 1);
    bNode.insert(new DefaultMutableTreeNode("1"), 0);
    bNode.insert(new DefaultMutableTreeNode("2"), 1);
    cNode.insert(new DefaultMutableTreeNode("1"), 0);
    cNode.insert(new DefaultMutableTreeNode("2"), 1);

    final DefaultTreeModel model = new DefaultTreeModel(root);
    final JTree tree = new JTree(model);

    final JTextField nameField = new JTextField("Z");
    final JButton button = new JButton("Add");
    button.setEnabled(false);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TreePath tp = tree.getSelectionPath();
            MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex = 0;
            if (insertNode.getParent() != null) {
                MutableTreeNode parent = (MutableTreeNode) insertNode.getParent();
                insertIndex = parent.getIndex(insertNode) + 1;
                insertNode = parent;
            }
            MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText());
            model.insertNodeInto(node, insertNode, insertIndex);
        }
    });
    JPanel addPanel = new JPanel(new GridLayout(2, 1));
    addPanel.add(nameField);
    addPanel.add(button);

    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            TreePath tp = e.getNewLeadSelectionPath();
            button.setEnabled(tp != null);
        }
    });

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.getContentPane().add(new JScrollPane(tree));
    frame.getContentPane().add(addPanel, BorderLayout.SOUTH);
    frame.setVisible(true);
}

From source file:MyTreeModelListener.java

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

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    JTree tree = new JTree(root);

    DefaultTreeModel treeModel = new DefaultTreeModel(root);
    treeModel.addTreeModelListener(new MyTreeModelListener());

    treeModel.insertNodeInto(new DefaultMutableTreeNode("A"), root, 0);

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

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);//  w  ww  .j  av  a  2 s.c om
    frame.setVisible(true);

}

From source file:MainClass.java

public static void main(String[] args) {
    MutableTreeNode root = new DefaultMutableTreeNode("A");
    MutableTreeNode beams = new DefaultMutableTreeNode("B");
    MutableTreeNode gears = new DefaultMutableTreeNode("C");
    root.insert(beams, 0);/* www  . ja v a  2  s  .c  o  m*/
    root.insert(gears, 1);
    beams.insert(new DefaultMutableTreeNode("4 "), 0);
    beams.insert(new DefaultMutableTreeNode("6 "), 1);
    beams.insert(new DefaultMutableTreeNode("8 "), 2);
    beams.insert(new DefaultMutableTreeNode("12 "), 3);
    gears.insert(new DefaultMutableTreeNode("8t"), 0);
    gears.insert(new DefaultMutableTreeNode("24t"), 1);
    gears.insert(new DefaultMutableTreeNode("40t"), 2);

    final DefaultTreeModel model = new DefaultTreeModel(root);
    final JTree tree = new JTree(model);

    final JTextField nameField = new JTextField("16t");
    final JButton button = new JButton("Add a part");
    button.setEnabled(false);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TreePath tp = tree.getSelectionPath();
            MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex = 0;
            if (insertNode.getParent() != null) {
                MutableTreeNode parent = (MutableTreeNode) insertNode.getParent();
                insertIndex = parent.getIndex(insertNode) + 1;
                insertNode = parent;
            }
            MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText());
            model.insertNodeInto(node, insertNode, insertIndex);
        }
    });
    JPanel addPanel = new JPanel(new GridLayout(2, 1));
    addPanel.add(nameField);
    addPanel.add(button);

    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            TreePath tp = e.getNewLeadSelectionPath();
            button.setEnabled(tp != null);
        }
    });

    JFrame frame = new JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.getContentPane().add(new JScrollPane(tree));
    frame.getContentPane().add(addPanel, BorderLayout.SOUTH);
    frame.setVisible(true);
}

From source file:SimpleClient.java

public static void main(String argv[]) {
    boolean usage = false;

    for (int optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-L")) {
            url.addElement(argv[++optind]);
        } else if (argv[optind].startsWith("-")) {
            usage = true;// www .j  a va2s. c o m
            break;
        } else {
            usage = true;
            break;
        }
    }

    if (usage || url.size() == 0) {
        System.out.println("Usage: SimpleClient -L url");
        System.out.println("   where url is protocol://username:password@hostname/");
        System.exit(1);
    }

    try {
        // Set up our Mailcap entries.  This will allow the JAF
        // to locate our viewers.
        File capfile = new File("simple.mailcap");
        if (!capfile.isFile()) {
            System.out.println("Cannot locate the \"simple.mailcap\" file.");
            System.exit(1);
        }

        CommandMap.setDefaultCommandMap(new MailcapCommandMap(new FileInputStream(capfile)));

        JFrame frame = new JFrame("Simple JavaMail Client");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // Get a Store object
        SimpleAuthenticator auth = new SimpleAuthenticator(frame);
        Session session = Session.getDefaultInstance(System.getProperties(), auth);
        //session.setDebug(true);

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

        // create a node for each store we have
        for (Enumeration e = url.elements(); e.hasMoreElements();) {
            String urlstring = (String) e.nextElement();
            URLName urln = new URLName(urlstring);
            Store store = session.getStore(urln);

            StoreTreeNode storenode = new StoreTreeNode(store);
            root.add(storenode);
        }

        DefaultTreeModel treeModel = new DefaultTreeModel(root);
        JTree tree = new JTree(treeModel);
        tree.addTreeSelectionListener(new TreePress());

        /* Put the Tree in a scroller. */
        JScrollPane sp = new JScrollPane();
        sp.setPreferredSize(new Dimension(250, 300));
        sp.getViewport().add(tree);

        /* Create a double buffered JPanel */
        JPanel sv = new JPanel(new BorderLayout());
        sv.add("Center", sp);

        fv = new FolderViewer(null);

        JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sv, fv);
        jsp.setOneTouchExpandable(true);
        mv = new MessageViewer();
        JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jsp, mv);
        jsp2.setOneTouchExpandable(true);

        frame.getContentPane().add(jsp2);
        frame.pack();
        frame.show();

    } catch (Exception ex) {
        System.out.println("SimpletClient caught exception");
        ex.printStackTrace();
        System.exit(1);
    }
}