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 void actionPerformed(ActionEvent ae) {
    DefaultMutableTreeNode dmtn, node;

    TreePath path = this.getSelectionPath();
    dmtn = (DefaultMutableTreeNode) path.getLastPathComponent();
    if (ae.getActionCommand().equals("insert")) {
        node = new DefaultMutableTreeNode("children");
        dmtn.add(node);
        ((DefaultTreeModel) this.getModel()).nodeStructureChanged((TreeNode) dmtn);
    }//from   www  .j a v a2  s. c o m
    if (ae.getActionCommand().equals("remove")) {
        node = (DefaultMutableTreeNode) dmtn.getParent();
        int nodeIndex = node.getIndex(dmtn);
        dmtn.removeAllChildren();
        node.remove(nodeIndex);
        ((DefaultTreeModel) this.getModel()).nodeStructureChanged((TreeNode) dmtn);
    }
}

From source file:FileTree.java

/** Add nodes from under "dir" into curTop. Highly recursive. */
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
    String curPath = dir.getPath();
    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
    if (curTop != null) { // should only be null at root
        curTop.add(curDir);
    }/*from   w  w w  .  j  a va2 s  .  c  om*/
    Vector ol = new Vector();
    String[] tmp = dir.list();
    for (int i = 0; i < tmp.length; i++)
        ol.addElement(tmp[i]);
    Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
    File f;
    Vector files = new Vector();
    // Make two passes, one for Dirs and one for Files. This is #1.
    for (int i = 0; i < ol.size(); i++) {
        String thisObject = (String) ol.elementAt(i);
        String newPath;
        if (curPath.equals("."))
            newPath = thisObject;
        else
            newPath = curPath + File.separator + thisObject;
        if ((f = new File(newPath)).isDirectory())
            addNodes(curDir, f);
        else
            files.addElement(thisObject);
    }
    // Pass two: for files.
    for (int fnum = 0; fnum < files.size(); fnum++)
        curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
    return curDir;
}

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);
    node.add(new DefaultMutableTreeNode("Borman"));
    node.add(new DefaultMutableTreeNode("Lovell"));
    node.add(new DefaultMutableTreeNode("Anders"));

    node = new DefaultMutableTreeNode("Apollo 11");
    rootNode.add(node);//from  w  w  w .  j ava2  s  . c o m
    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:MyTree.java

public boolean importData(JComponent comp, Transferable t) {
    if (!(comp instanceof MyTree)) {
        return false;
    }// www .  ja v  a 2s.  c  om
    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:MainClass.java

public void init() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
    DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
    DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");

    treeModel = new DefaultTreeModel(root);
    tree = new JTree(treeModel);

    treeModel.insertNodeInto(subroot, root, 0);
    subroot.add(leaf1);
    root.add(leaf2);/*from  w w w .j a v  a 2 s.co m*/

    tree.putClientProperty("JTree.lineStyle", "Angled");
    getContentPane().add(tree, BorderLayout.CENTER);
}

From source file:MainClass.java

public void init() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
    DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
    DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");

    treeModel = new DefaultTreeModel(root);
    tree = new JTree(treeModel);

    treeModel.insertNodeInto(subroot, root, 0);
    subroot.add(leaf1);
    root.add(leaf2);/*from   w  w  w  .ja v a2s.  c o  m*/

    tree.putClientProperty("JTree.lineStyle", "Angled");
    getContentPane().add(tree, BorderLayout.CENTER);

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    renderer.setClosedIcon(new ImageIcon("door.closed.gif"));
    renderer.setOpenIcon(new ImageIcon("door.open.gif"));
    renderer.setLeafIcon(new ImageIcon("world.gif"));

}

From source file:net.sf.housekeeper.swing.CategoriesView.java

private DefaultMutableTreeNode createNode(final Category cat) {
    final DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(cat);

    final Iterator catIter = cat.getChildrenIterator();
    while (catIter.hasNext()) {
        Category element = (Category) catIter.next();
        rootNode.add(createNode(element));
    }/*from  w  w w .  jav a2s .  c  o m*/

    return rootNode;
}

From source file:MyData.java

protected MutableTreeNode getRootNode() {
    DefaultMutableTreeNode root, child;
    MyData question;/*from w ww .j av a2  s  . c o m*/
    root = new DefaultMutableTreeNode("Root");
    for (int i = 0; i < questions.length; i++) {
        question = new MyData(questions[i]);
        child = new DefaultMutableTreeNode(question);
        root.add(child);
    }
    return root;
}

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);
        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");
        {// w ww  .  j a v  a2s  .  c  o  m
            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:net.landora.animeinfo.notifications.NotificationViewer.java

/**
 * Creates new form NotificationViewer//from   w  w w.  j  ava2 s .c  o  m
 */
public NotificationViewer() {
    initComponents();

    setTitle("Anime Notifications");

    DefaultMutableTreeNode root = new DefaultMutableTreeNode();

    notificationsNode = new DefaultMutableTreeNode("Notifications");
    root.add(notificationsNode);

    treeModel = new DefaultTreeModel(root);

    treeItems.setModel(treeModel);
    //        LazyTreeLoadingManager.getInstance().setup(treeItems);

    VideoManagerApp.getInstance().getEventBus().addHandlersWeak(this);
}