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: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);/*from w w w . j av  a2 s.com*/
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    CheckBoxNode accessibilityOptions[] = { new CheckBoxNode("A", false), new CheckBoxNode("B", true) };
    CheckBoxNode browsingOptions[] = { new CheckBoxNode("C", true), new CheckBoxNode("D", true),
            new CheckBoxNode("E", true), new CheckBoxNode("F", false) };
    Vector accessVector = new NamedVector("G", accessibilityOptions);
    Vector browseVector = new NamedVector("H", browsingOptions);
    Object rootNodes[] = { accessVector, browseVector };
    Vector rootVector = new NamedVector("Root", rootNodes);
    tree = new JTree(rootVector);

    CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
    tree.setCellRenderer(renderer);//w ww.  jav  a  2  s  .  com

    tree.setCellEditor(new CheckBoxNodeEditor(tree));
    tree.setEditable(true);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.NORTH);

    JPanel buttonPanel = new JPanel();
    JButton button = new JButton("new node");
    buttonPanel.add(button);
    frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    button.addActionListener(e -> {
        DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New node");
        root.add(newNode);
        model.reload();
    });

    frame.setSize(300, 450);
    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;//w w w .  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);
    }
}

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  ww  .  j  a  v a2s.co  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

private static DefaultTreeModel buildDemoModel() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

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

    return new DefaultTreeModel(root);
}

From source file:Main.java

public static DefaultTreeModel buildDemoModel() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode("A");
    childNode.add(new DefaultMutableTreeNode("A1"));
    childNode.add(new DefaultMutableTreeNode("A2"));
    root.add(childNode);//from   w ww .  j  a va  2 s.c om

    childNode = new DefaultMutableTreeNode("B");
    childNode.add(new DefaultMutableTreeNode("B1"));
    childNode.add(new DefaultMutableTreeNode("B2"));
    root.add(childNode);

    return new DefaultTreeModel(root);
}

From source file:Main.java

public static DefaultMutableTreeNode addNodes(File dir) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(dir);
    for (File file : dir.listFiles()) {
        if (file.isDirectory()) {
            node.add(addNodes(file));
        } else {/*from  w  w  w.j  a  va2 s . co  m*/
            node.add(new DefaultMutableTreeNode(file));
        }
    }
    return node;
}

From source file:Main.java

private static DefaultMutableTreeNode addCloneNode(DefaultMutableTreeNode srcNode,
        DefaultMutableTreeNode root) {
    DefaultMutableTreeNode clone = new DefaultMutableTreeNode(srcNode.getUserObject());
    root.add(clone);
    for (int i = 0; i < srcNode.getChildCount(); i++) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) srcNode.getChildAt(i);
        addCloneNode(child, clone);/*  w w w  .j a v  a  2 s.  c  o  m*/
    }
    return clone;
}

From source file:Main.java

static DefaultMutableTreeNode pathToTreeNode(Object[] objects, int offset) {
    if (objects == null || objects.length == 0 || offset >= objects.length || offset < 0) {
        return null;
    }//from  w ww .j  a  va  2s.  c  o m
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(objects[offset]);
    DefaultMutableTreeNode child = pathToTreeNode(objects, offset + 1);
    if (child != null) {
        node.add(child);
    }
    return node;
}

From source file:Main.java

static DefaultMutableTreeNode createTreeModel(File file, boolean recursive) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(file);
    if (file.isDirectory() && recursive) {
        File[] children = file.listFiles();
        if (children != null) {
            for (File f : children) {
                node.add(createTreeModel(f, recursive));
            }/*www . j  a  v  a 2  s  .  c o  m*/
        }
    }
    return node;
}