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:UndoExample4.java

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

    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  ww  .j av a  2 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"));

    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:BasicDnD.java

public BasicDnD() {
    super(new BorderLayout());
    JPanel leftPanel = createVerticalBoxPanel();
    JPanel rightPanel = createVerticalBoxPanel();

    //Create a table model.
    DefaultTableModel tm = new DefaultTableModel();
    tm.addColumn("Column 0");
    tm.addColumn("Column 1");
    tm.addColumn("Column 2");
    tm.addColumn("Column 3");
    tm.addRow(new String[] { "Table 00", "Table 01", "Table 02", "Table 03" });
    tm.addRow(new String[] { "Table 10", "Table 11", "Table 12", "Table 13" });
    tm.addRow(new String[] { "Table 20", "Table 21", "Table 22", "Table 23" });
    tm.addRow(new String[] { "Table 30", "Table 31", "Table 32", "Table 33" });

    //LEFT COLUMN
    //Use the table model to create a table.
    table = new JTable(tm);
    leftPanel.add(createPanelForComponent(table, "JTable"));

    //Create a color chooser.
    colorChooser = new JColorChooser();
    leftPanel.add(createPanelForComponent(colorChooser, "JColorChooser"));

    //RIGHT COLUMN
    //Create a textfield.
    textField = new JTextField(30);
    textField.setText("Favorite foods:\nPizza, Moussaka, Pot roast");
    rightPanel.add(createPanelForComponent(textField, "JTextField"));

    //Create a scrolled text area.
    textArea = new JTextArea(5, 30);
    textArea.setText("Favorite shows:\nBuffy, Alias, Angel");
    JScrollPane scrollPane = new JScrollPane(textArea);
    rightPanel.add(createPanelForComponent(scrollPane, "JTextArea"));

    //Create a list model and a list.
    DefaultListModel listModel = new DefaultListModel();
    listModel.addElement("Martha Washington");
    listModel.addElement("Abigail Adams");
    listModel.addElement("Martha Randolph");
    listModel.addElement("Dolley Madison");
    listModel.addElement("Elizabeth Monroe");
    listModel.addElement("Louisa Adams");
    listModel.addElement("Emily Donelson");
    list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    JScrollPane listView = new JScrollPane(list);
    listView.setPreferredSize(new Dimension(300, 100));
    rightPanel.add(createPanelForComponent(listView, "JList"));

    //Create a tree.
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Mia Familia");
    DefaultMutableTreeNode sharon = new DefaultMutableTreeNode("Sharon");
    rootNode.add(sharon);
    DefaultMutableTreeNode maya = new DefaultMutableTreeNode("Maya");
    sharon.add(maya);/* www.  j  a  v  a2 s. co m*/
    DefaultMutableTreeNode anya = new DefaultMutableTreeNode("Anya");
    sharon.add(anya);
    sharon.add(new DefaultMutableTreeNode("Bongo"));
    maya.add(new DefaultMutableTreeNode("Muffin"));
    anya.add(new DefaultMutableTreeNode("Winky"));
    DefaultTreeModel model = new DefaultTreeModel(rootNode);
    tree = new JTree(model);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    JScrollPane treeView = new JScrollPane(tree);
    treeView.setPreferredSize(new Dimension(300, 100));
    rightPanel.add(createPanelForComponent(treeView, "JTree"));

    //Create the toggle button.
    toggleDnD = new JCheckBox("Turn on Drag and Drop");
    toggleDnD.setActionCommand("toggleDnD");
    toggleDnD.addActionListener(this);

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
    splitPane.setOneTouchExpandable(true);

    add(splitPane, BorderLayout.CENTER);
    add(toggleDnD, BorderLayout.PAGE_END);
    setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
}

From source file:org.syncope.console.commons.RoleTreeBuilder.java

private void populateSubtree(final DefaultMutableTreeNode subRoot, final List<RoleTO> roles) {

    RoleTO role = (RoleTO) subRoot.getUserObject();

    DefaultMutableTreeNode child;
    for (RoleTO subRoleTO : getChildRoles(role.getId(), roles)) {
        child = new DefaultMutableTreeNode(subRoleTO);
        subRoot.add(child);
        populateSubtree(child, roles);//  w w w .j a v  a 2s . com
    }
}

From source file:ListAlgorithmsBC.java

private void getNodes(DefaultMutableTreeNode providerNode, Provider provider, Set<Provider.Service> used,
        String title, String target) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(title);
    for (Provider.Service service : provider.getServices()) {
        if (!used.contains(service) && target.equalsIgnoreCase(service.getType())) {
            used.add(service);//from   ww  w .  j  a v a  2 s  .  c  om
            DefaultMutableTreeNode algNode = new DefaultMutableTreeNode(service.getAlgorithm());
            node.add(algNode);
            algNode.add(new DefaultMutableTreeNode("class : " + service.getClassName()));
        }
    }
    if (node.getChildCount() != 0) {
        providerNode.add(node);
    }
}

From source file:com.github.rholder.gradle.ui.DependencyViewerStandalone.java

public void updateView(GradleNode rootDependency, GradleNode selectedDependency) {
    // TODO replace this hack with something that populates the GradleNode graph

    DefaultMutableTreeNode fullRoot = new DefaultMutableTreeNode(new GradleNode("Project Dependencies"));
    if (rootDependency == null) {
        DefaultMutableTreeNode loading = new DefaultMutableTreeNode(new GradleNode("Loading..."));
        fullRoot.add(loading);
    } else {/*from   w  ww .  j a  v a 2s . com*/
        DefaultMutableTreeNode flattenedRoot = convertToSortedTreeNode(rootDependency);
        DefaultMutableTreeNode hierarchyRoot = convertToHierarchyTreeNode(rootDependency);
        fullRoot.add(flattenedRoot);
        fullRoot.add(hierarchyRoot);
    }

    TreeModel treeModel = new DefaultTreeModel(fullRoot);
    final JTree fullTree = new JTree(treeModel);
    fullTree.setCellRenderer(dependencyCellRenderer);

    // expand path for first level from root
    //fullTree.expandPath(new TreePath(hierarchyRoot.getNextNode().getPath()));

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            if (gradleBaseDir != null) {
                setTitle(TITLE + " - " + gradleBaseDir);
            }
            splitter.setLeftComponent(new JScrollPane(fullTree));
            splitter.setRightComponent(new JScrollPane(information));
            splitter.setDividerLocation(0.75);
        }
    });
}

From source file:Main.java

private DefaultMutableTreeNode builtTreeNode(Node root) {
    DefaultMutableTreeNode dmtNode;

    dmtNode = new DefaultMutableTreeNode(root.getNodeName());
    NodeList nodeList = root.getChildNodes();
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);

        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            if (tempNode.hasChildNodes()) {
                dmtNode.add(builtTreeNode(tempNode));
            }//from www . j  ava  2s  .c o m
        }
    }
    return dmtNode;
}

From source file:com.autentia.wuija.widget.TreeComponent.java

private void buildTree(String name, List<OperationalTraceParams> operationalTraceParams) {
    final DefaultMutableTreeNode rootTreeNode = createSingleNode(name, false, true);
    model = new DefaultTreeModel(rootTreeNode);

    for (OperationalTraceParams trace : operationalTraceParams) {
        if (trace.getSubParamValues() == null) {
            rootTreeNode.add(createSingleNode(trace));
        } else {//from   w  ww.ja  v a 2  s  . c o  m
            rootTreeNode.add(createChildNodes(trace));
        }
    }
}

From source file:Main.java

void createNodes(DefaultMutableTreeNode top) {
    DefaultMutableTreeNode contactName;
    List<Contact> contacts = new ArrayList<Contact>();
    contacts.add(new Contact("Me", true));
    contacts.add(new Contact("You"));

    Iterator<Contact> contactItr = contacts.iterator();
    while (contactItr.hasNext()) {
        contactName = new DefaultMutableTreeNode(contactItr.next());
        top.add(contactName);
    }//from   w w  w .j a  v a2  s  . c om
}

From source file:Main.java

private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
    DefaultMutableTreeNode child;
    for (int i = 1; i < hierarchy.length; i++) {
        Object nodeSpecifier = hierarchy[i];
        if (nodeSpecifier instanceof Object[]) //  node with children
            child = processHierarchy((Object[]) nodeSpecifier);
        else/*from www . j av  a 2s.  c  o m*/
            child = new DefaultMutableTreeNode(nodeSpecifier); //  Leaf
        node.add(child);
    }
    return (node);
}

From source file:TreeIconDemo.java

private void createNodes(DefaultMutableTreeNode top) {
    DefaultMutableTreeNode category = null;
    DefaultMutableTreeNode book = null;

    category = new DefaultMutableTreeNode("Books for Java Programmers");
    top.add(category);

    // original Tutorial
    book = new DefaultMutableTreeNode(
            new BookInfo("The Java Tutorial: A Short Course on the Basics", "tutorial.html"));
    category.add(book);//from ww w.  j  a v a 2 s  . c  o  m

    // Tutorial Continued
    book = new DefaultMutableTreeNode(
            new BookInfo("The Java Tutorial Continued: The Rest of the JDK", "tutorialcont.html"));
    category.add(book);

    // JFC Swing Tutorial
    book = new DefaultMutableTreeNode(
            new BookInfo("The JFC Swing Tutorial: A Guide to Constructing GUIs", "swingtutorial.html"));
    category.add(book);

    // Bloch
    book = new DefaultMutableTreeNode(new BookInfo("Effective Java Programming Language Guide", "bloch.html"));
    category.add(book);

    // Arnold/Gosling
    book = new DefaultMutableTreeNode(new BookInfo("The Java Programming Language", "arnold.html"));
    category.add(book);

    // Chan
    book = new DefaultMutableTreeNode(new BookInfo("The Java Developers Almanac", "chan.html"));
    category.add(book);

    category = new DefaultMutableTreeNode("Books for Java Implementers");
    top.add(category);

    // VM
    book = new DefaultMutableTreeNode(new BookInfo("The Java Virtual Machine Specification", "vm.html"));
    category.add(book);

    // Language Spec
    book = new DefaultMutableTreeNode(new BookInfo("The Java Language Specification", "jls.html"));
    category.add(book);
}