Example usage for javax.swing.tree DefaultMutableTreeNode DefaultMutableTreeNode

List of usage examples for javax.swing.tree DefaultMutableTreeNode DefaultMutableTreeNode

Introduction

In this page you can find the example usage for javax.swing.tree DefaultMutableTreeNode DefaultMutableTreeNode.

Prototype

public DefaultMutableTreeNode(Object userObject) 

Source Link

Document

Creates a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.

Usage

From source file:EditableTree.java

public EditableTree() {
    DefaultMutableTreeNode forums = new DefaultMutableTreeNode("A");
    forums.add(new DefaultMutableTreeNode("B"));
    DefaultMutableTreeNode articles = new DefaultMutableTreeNode("E");
    articles.add(new DefaultMutableTreeNode("F"));
    DefaultMutableTreeNode examples = new DefaultMutableTreeNode("G");
    examples.add(new DefaultMutableTreeNode("H"));

    m_rootNode.add(forums);//from  w w  w.  j a va 2  s  .  com
    m_rootNode.add(articles);
    m_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.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();
            if (selNode == null) {
                return;
            }
            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node");
            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);

    m_delButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();
            removeNode(selNode);
        }
    });
    panel.add(m_delButton);

    JPanel searchPanel = new JPanel();
    searchPanel.setBorder(BorderFactory.createEtchedBorder());

    m_searchText = new JTextField(10);
    searchPanel.add(m_searchText);

    m_searchButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode node = searchNode(m_searchText.getText());
            if (node != null) {
                TreeNode[] nodes = m_model.getPathToRoot(node);
                TreePath path = new TreePath(nodes);
                m_tree.scrollPathToVisible(path);
                m_tree.setSelectionPath(path);
            } else {
                System.out.println("Node with string " + m_searchText.getText() + " not found");
            }
        }
    });
    searchPanel.add(m_searchButton);

    m_searchAndDeleteButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode node = searchNode(m_searchText.getText());
            if (node != null) {
                removeNode(node);
            } else {
                System.out.println("Node with string " + m_searchText.getText() + " not found");
            }
        }
    });
    searchPanel.add(m_searchAndDeleteButton);
    panel.add(searchPanel);
    getContentPane().add(panel, BorderLayout.SOUTH);
    setSize(700, 400);
    setVisible(true);
}

From source file:MyTree.java

public boolean importData(JComponent comp, Transferable t) {
    if (!(comp instanceof MyTree)) {
        return false;
    }//from w  w w  .ja v a2 s .  com
    if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        return false;
    }
    MyTree tree = (MyTree) comp;
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    try {
        List data = (List) t.getTransferData(DataFlavor.javaFileListFlavor);
        Iterator i = data.iterator();
        while (i.hasNext()) {
            File f = (File) i.next();
            root.add(new DefaultMutableTreeNode(f.getName()));
        }
        model.reload();
        return true;
    } catch (Exception ioe) {
        System.out.println(ioe);
    }
    return false;
}

From source file:UndoExample3.java

public UndoExample3() {
    super("Undo/Redo Example 3");

    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root");
    DefaultMutableTreeNode node = new DefaultMutableTreeNode("Apollo 8");
    rootNode.add(node);/*from  w  w w  .j a  v a  2 s .c  om*/
    node.add(new DefaultMutableTreeNode("Borman"));
    node.add(new DefaultMutableTreeNode("Lovell"));
    node.add(new DefaultMutableTreeNode("Anders"));

    node = new DefaultMutableTreeNode("Apollo 11");
    rootNode.add(node);
    node.add(new DefaultMutableTreeNode("Armstrong"));
    node.add(new DefaultMutableTreeNode("Aldrin"));
    node.add(new DefaultMutableTreeNode("Collins"));

    node = new DefaultMutableTreeNode("Apollo 12");
    rootNode.add(node);
    node.add(new DefaultMutableTreeNode("Conrad"));
    node.add(new DefaultMutableTreeNode("Gordon"));
    node.add(new DefaultMutableTreeNode("Bean"));

    UndoableTree tree = new UndoableTree(rootNode);

    getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);

    // Create the undo manager and actions
    UndoManager manager = new UndoManager();
    tree.addUndoableEditListener(manager);

    Action undoAction = new UndoAction(manager);
    Action redoAction = new RedoAction(manager);

    // Add the actions to buttons
    JPanel panel = new JPanel();
    JButton undoButton = new JButton("Undo");
    JButton redoButton = new JButton("Redo");
    undoButton.addActionListener(undoAction);
    redoButton.addActionListener(redoAction);
    panel.add(undoButton);
    panel.add(redoButton);
    getContentPane().add(panel, BorderLayout.SOUTH);

    // Assign the actions to keys
    ((JComponent) getContentPane()).registerKeyboardAction(undoAction,
            KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
    ((JComponent) getContentPane()).registerKeyboardAction(redoAction,
            KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
}

From source file:MainClass.java

protected void fillModel(SortTreeModel model, DefaultMutableTreeNode current) {
    MyFile pf = (MyFile) current.getUserObject();
    File f = pf.getFile();/*from w  w  w. j a v  a2s . com*/
    if (f.isDirectory()) {
        String files[] = f.list();
        for (int i = 0; i < files.length; i++) {
            if (files[i].startsWith("."))
                continue;
            MyFile tmp = new MyFile(pf, files[i]);
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(tmp);
            model.insertNodeInto(node, current);
            if (tmp.getFile().isDirectory()) {
                fillModel(model, node);
            }
        }
    }
}

From source file:FileTree3.java

public FileTree3() {
    super("Directories Tree [Tool Tips]");
    setSize(400, 300);//from   w  w  w.  j a  v a  2 s. co 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);
    // NEW
    m_tree = new JTree(m_model) {
        public String getToolTipText(MouseEvent ev) {
            if (ev == null)
                return null;
            TreePath path = m_tree.getPathForLocation(ev.getX(), ev.getY());
            if (path != null) {
                FileNode fnode = getFileNode(getTreeNode(path));
                if (fnode == null)
                    return null;
                File f = fnode.getFile();
                return (f == null ? null : f.getPath());
            }
            return null;
        }
    };
    ToolTipManager.sharedInstance().registerComponent(m_tree);

    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);

    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(FileTree3.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(FileTree3.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);
}

From source file:UndoExample4.java

public UndoExample4() {
    super("Undo/Redo Example 4");

    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root");
    DefaultMutableTreeNode node = new DefaultMutableTreeNode("Apollo 8");
    rootNode.add(node);//from   w  w  w  .  ja v a 2  s  . c  om
    node.add(new DefaultMutableTreeNode("Borman"));
    node.add(new DefaultMutableTreeNode("Lovell"));
    node.add(new DefaultMutableTreeNode("Anders"));

    node = new DefaultMutableTreeNode("Apollo 11");
    rootNode.add(node);
    node.add(new DefaultMutableTreeNode("Armstrong"));
    node.add(new DefaultMutableTreeNode("Aldrin"));
    node.add(new DefaultMutableTreeNode("Collins"));

    node = new DefaultMutableTreeNode("Apollo 12");
    rootNode.add(node);
    node.add(new DefaultMutableTreeNode("Conrad"));
    node.add(new DefaultMutableTreeNode("Gordon"));
    node.add(new DefaultMutableTreeNode("Bean"));

    UndoableTree2 tree = new UndoableTree2(rootNode);

    getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);

    // Create the undo manager and actions
    UndoManager manager = new UndoManager();
    tree.addUndoableEditListener(manager);

    Action undoAction = new UndoAction(manager);
    Action redoAction = new RedoAction(manager);

    // Add the actions to buttons
    JPanel panel = new JPanel();
    JButton undoButton = new JButton("Undo");
    JButton redoButton = new JButton("Redo");
    undoButton.addActionListener(undoAction);
    redoButton.addActionListener(redoAction);
    panel.add(undoButton);
    panel.add(redoButton);
    getContentPane().add(panel, BorderLayout.SOUTH);

    // Assign the actions to keys
    ((JComponent) getContentPane()).registerKeyboardAction(undoAction,
            KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
    ((JComponent) getContentPane()).registerKeyboardAction(redoAction,
            KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
}

From source file:ListAlgorithmsBC.java

private ListAlgorithmsBC() {
    super("JCE Algorithms");
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Providers");
    DefaultTreeModel treeModel = new DefaultTreeModel(root);
    for (Provider provider : java.security.Security.getProviders()) {
        DefaultMutableTreeNode providerNode = new DefaultMutableTreeNode(provider);
        root.add(providerNode);/*from   www  . ja va2 s .c o  m*/
        Set<Provider.Service> used = new HashSet<Provider.Service>();

        getNodes(providerNode, provider, used, "Cipher", "cipher");
        getNodes(providerNode, provider, used, "Key Agreement", "keyagreement");
        getNodes(providerNode, provider, used, "Key Generator", "keygenerator");
        getNodes(providerNode, provider, used, "Key Pair Generator", "keypairgenerator");
        getNodes(providerNode, provider, used, "Key Factory", "keyfactory");
        getNodes(providerNode, provider, used, "Secret Key Factory", "secretkeyfactory");
        getNodes(providerNode, provider, used, "Mac", "mac");
        getNodes(providerNode, provider, used, "Message Digest", "messagedigest");
        getNodes(providerNode, provider, used, "Signature", "signature");
        getNodes(providerNode, provider, used, "Algorithm Paramater", "algorithmparameters");
        getNodes(providerNode, provider, used, "Algorithm Paramater Generator", "algorithmparametergenerator");
        getNodes(providerNode, provider, used, "Key Store", "keystore");
        getNodes(providerNode, provider, used, "Secure Random", "securerandom");
        getNodes(providerNode, provider, used, "Certificate Factory", "certificatefactory");
        getNodes(providerNode, provider, used, "Certificate Store", "certstore");
        getNodes(providerNode, provider, used, "Key Manager Factory", "KeyManagerFactory");
        getNodes(providerNode, provider, used, "Trust Manager Factory", "TrustManagerFactory");
        getNodes(providerNode, provider, used, "SSL Context", "SSLContext");
        getNodes(providerNode, provider, used, "Sasl Server Factory", "SaslServerFactory");
        getNodes(providerNode, provider, used, "Sasl Client Factory", "SaslClientFactory");
        {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode("Other");
            for (Provider.Service service : provider.getServices()) {
                if (!used.contains(service)) {
                    DefaultMutableTreeNode serviceNode = new DefaultMutableTreeNode(
                            service.getType() + " : " + service.getAlgorithm());
                    node.add(serviceNode);
                    serviceNode.add(new DefaultMutableTreeNode("class : " + service.getClassName()));
                }
            }
            if (node.getChildCount() != 0)
                providerNode.add(node);
        }
    }

    JTree tree = new JTree(treeModel);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setEditable(false);
    JScrollPane pane = new JScrollPane(tree);
    pane.setPreferredSize(new Dimension(200, 200));

    getContentPane().add(pane);

    pack();
}

From source file:TreeIt.java

public TreeIt() {
    JFrame f = new JFrame();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar");
    DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months");
    root.add(months);//www  . j  ava 2s  . c  o  m
    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);
    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:Main.java

DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(dir);
    if (curTop != null) {
        curTop.add(curDir);//from w  w  w . j  a  v  a  2  s.c om
    }
    File[] tmp = dir.listFiles();
    Vector<File> ol = new Vector<File>();
    ol.addAll(Arrays.asList(tmp));
    Collections.sort(ol, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {
            int result = o1.getName().compareTo(o2.getName());
            if (o1.isDirectory() && o2.isFile()) {
                result = -1;
            } else if (o2.isDirectory() && o1.isFile()) {
                result = 1;
            }
            return result;
        }
    });
    for (int i = 0; i < ol.size(); i++) {
        File file = ol.elementAt(i);
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(file);
        if (file.isDirectory()) {
            addNodes(node, file);
        }
        curDir.add(node);
    }
    return curDir;
}

From source file:Main.java

public MyTreeModel() {
    super(new DefaultMutableTreeNode("root"));
    rootNode = (DefaultMutableTreeNode) getRoot();
}