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

public TreeIconDemo() {
    super(new GridLayout(1, 0));

    // Create the nodes.
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);//w w  w . j ava 2s .co  m

    // Create a tree that allows one selection at a time.
    tree = new JTree(top);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    // Set the icon for leaf nodes.
    ImageIcon leafIcon = createImageIcon("images/middle.gif");
    if (leafIcon != null) {
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
        renderer.setLeafIcon(leafIcon);
        tree.setCellRenderer(renderer);
    } else {
        System.err.println("Leaf icon missing; using default.");
    }

    // Listen for when the selection changes.
    tree.addTreeSelectionListener(this);

    // Create the scroll pane and add the tree to it.
    JScrollPane treeView = new JScrollPane(tree);

    // Create the HTML viewing pane.
    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    initHelp();
    JScrollPane htmlView = new JScrollPane(htmlPane);

    // Add the scroll panes to a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(treeView);
    splitPane.setBottomComponent(htmlView);

    Dimension minimumSize = new Dimension(100, 50);
    htmlView.setMinimumSize(minimumSize);
    treeView.setMinimumSize(minimumSize);
    splitPane.setDividerLocation(100); // XXX: ignored in some releases
    // of Swing. bug 4101306
    // workaround for bug 4101306:
    // treeView.setPreferredSize(new Dimension(100, 100));

    splitPane.setPreferredSize(new Dimension(500, 300));

    // Add the split pane to this panel.
    add(splitPane);
}

From source file:components.TreeIconDemo.java

public TreeIconDemo() {
    super(new GridLayout(1, 0));

    //Create the nodes.
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);/*from   w ww.ja  v  a 2s. c  om*/

    //Create a tree that allows one selection at a time.
    tree = new JTree(top);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    //Set the icon for leaf nodes.
    ImageIcon leafIcon = createImageIcon("images/middle.gif");
    if (leafIcon != null) {
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
        renderer.setLeafIcon(leafIcon);
        tree.setCellRenderer(renderer);
    } else {
        System.err.println("Leaf icon missing; using default.");
    }

    //Listen for when the selection changes.
    tree.addTreeSelectionListener(this);

    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane(tree);

    //Create the HTML viewing pane.
    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    initHelp();
    JScrollPane htmlView = new JScrollPane(htmlPane);

    //Add the scroll panes to a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(treeView);
    splitPane.setBottomComponent(htmlView);

    Dimension minimumSize = new Dimension(100, 50);
    htmlView.setMinimumSize(minimumSize);
    treeView.setMinimumSize(minimumSize);
    splitPane.setDividerLocation(100); //XXX: ignored in some releases
                                       //of Swing. bug 4101306
                                       //workaround for bug 4101306:
                                       //treeView.setPreferredSize(new Dimension(100, 100)); 

    splitPane.setPreferredSize(new Dimension(500, 300));

    //Add the split pane to this panel.
    add(splitPane);
}

From source file:JTreeDemo.java

/** Construct the object including its GUI */
public JTreeDemo() {
    super("JTreeDemo");
    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());

    root = new DefaultMutableTreeNode("root");

    child = new DefaultMutableTreeNode("Colors");
    root.add(child);//from w  w  w.j a v a  2  s. c o m
    child.add(new DefaultMutableTreeNode("Cyan"));
    child.add(new DefaultMutableTreeNode("Magenta"));
    child.add(new DefaultMutableTreeNode("Yellow"));
    child.add(new DefaultMutableTreeNode("Black"));

    myTree = new JTree(root);

    // cp.add(BorderLayout.CENTER, myTree);
    //JScrollPane scroller = new JScrollPane();
    //scroller.getViewport().add(myTree);
    JScrollPane scroller = new JScrollPane(myTree);
    cp.add(BorderLayout.CENTER, scroller);

    cp.add(BorderLayout.NORTH, addButton = new JButton("Add"));
    addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            // Insert more nodes into the tree
            child = new DefaultMutableTreeNode("Flavors");
            child.add(new DefaultMutableTreeNode("Java"));
            child.add(new DefaultMutableTreeNode("Espresso"));
            child.add(new DefaultMutableTreeNode("Hey Joe!"));
            child.add(new DefaultMutableTreeNode("Charcoal"));
            child.add(new DefaultMutableTreeNode("Paint Remover"));

            // Notify the model, which will add it and create an event, and
            // send it up the tree...

            ((DefaultTreeModel) myTree.getModel()).insertNodeInto(child, root, 0);
        }
    });

    cp.add(BorderLayout.SOUTH, quitButton = new JButton("Exit"));
    quitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            dispose();
            System.exit(0);
        }
    });
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            setVisible(false);
            dispose();
            System.exit(0);
        }
    });
    pack();
}

From source file:it.unibas.spicygui.controllo.datasource.operators.GenerateInstanceTree.java

private void visitNode(INode node) {
    DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(
            new TreeNodeAdapter(node, dataSource.getType()));
    if (logger.isDebugEnabled())
        logger.debug("Creato nuovo nodo: " + treeNode);
    if (treeRoot == null) {
        treeRoot = treeNode;//from  ww w.  ja v  a  2  s . co  m
    } else {
        currentTreeNode.add(treeNode);
    }
    currentTreeNode = treeNode;
    for (INode child : node.getChildren()) {
        child.accept(this);
        currentTreeNode = treeNode;
    }
}

From source file:XMLTreeView.java

private void attachAttributeList(DefaultMutableTreeNode node, Attributes atts) {
    for (int i = 0; i < atts.getLength(); i++) {
        String name = atts.getLocalName(i);
        String value = atts.getValue(name);
        node.add(new DefaultMutableTreeNode(name + " = " + value));
    }/*  w w  w  .ja  v  a2s  .c  o  m*/
}

From source file:components.TreeDemo.java

public TreeDemo() {
    super(new GridLayout(1, 0));

    //Create the nodes.
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);//  w  w w.  ja  v a 2  s  .co  m

    //Create a tree that allows one selection at a time.
    tree = new JTree(top);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    //Listen for when the selection changes.
    tree.addTreeSelectionListener(this);

    if (playWithLineStyle) {
        System.out.println("line style = " + lineStyle);
        tree.putClientProperty("JTree.lineStyle", lineStyle);
    }

    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane(tree);

    //Create the HTML viewing pane.
    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    initHelp();
    JScrollPane htmlView = new JScrollPane(htmlPane);

    //Add the scroll panes to a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(treeView);
    splitPane.setBottomComponent(htmlView);

    Dimension minimumSize = new Dimension(100, 50);
    htmlView.setMinimumSize(minimumSize);
    treeView.setMinimumSize(minimumSize);
    splitPane.setDividerLocation(100);
    splitPane.setPreferredSize(new Dimension(500, 300));

    //Add the split pane to this panel.
    add(splitPane);
}

From source file:TreeIconDemo2.java

public TreeIconDemo2() {
    super(new GridLayout(1, 0));

    //Create the nodes.
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);/*from  w w w .jav a 2 s  .c  o m*/

    //Create a tree that allows one selection at a time.
    tree = new JTree(top);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    //Enable tool tips.
    ToolTipManager.sharedInstance().registerComponent(tree);

    //Set the icon for leaf nodes.
    ImageIcon tutorialIcon = createImageIcon("images/middle.gif");
    if (tutorialIcon != null) {
        tree.setCellRenderer(new MyRenderer(tutorialIcon));
    } else {
        System.err.println("Tutorial icon missing; using default.");
    }

    //Listen for when the selection changes.
    tree.addTreeSelectionListener(this);

    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane(tree);

    //Create the HTML viewing pane.
    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    initHelp();
    JScrollPane htmlView = new JScrollPane(htmlPane);

    //Add the scroll panes to a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(treeView);
    splitPane.setBottomComponent(htmlView);

    Dimension minimumSize = new Dimension(100, 50);
    htmlView.setMinimumSize(minimumSize);
    treeView.setMinimumSize(minimumSize);
    splitPane.setDividerLocation(100); //XXX: ignored in some releases
                                       //of Swing. bug 4101306
                                       //workaround for bug 4101306:
                                       //treeView.setPreferredSize(new Dimension(100, 100)); 

    splitPane.setPreferredSize(new Dimension(500, 300));

    //Add the split pane to this panel.
    add(splitPane);
}

From source file:gov.nij.er.ui.RawDataFilteredTreeModel.java

RawDataFilteredTreeModel(RecordTreeModel rawDataTreeModel) {
    this.rawDataTreeModel = rawDataTreeModel;
    rawDataTreeModel.addTreeModelListener(new TreeModelListener() {

        public void treeNodesChanged(TreeModelEvent e) {
            if (!isFiltered) {
                for (TreeModelListener l : listeners) {
                    l.treeNodesChanged(e);
                }/* w ww  . j a va2 s . co m*/
            }
        }

        public void treeNodesInserted(TreeModelEvent e) {
            if (!isFiltered) {
                for (TreeModelListener l : listeners) {
                    l.treeNodesInserted(e);
                }
            }
        }

        public void treeNodesRemoved(TreeModelEvent e) {
            if (!isFiltered) {
                for (TreeModelListener l : listeners) {
                    l.treeNodesRemoved(e);
                }
            }
        }

        public void treeStructureChanged(TreeModelEvent e) {
            if (!isFiltered) {
                for (TreeModelListener l : listeners) {
                    l.treeStructureChanged(e);
                }
            }
        }
    });
    root = new DefaultMutableTreeNode(ROOT_NODE_LABEL);

}

From source file:SwingDnDTest.java

public static JTree tree() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("World");
    DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA");
    root.add(country);/* ww  w.  j  av a 2 s. c o m*/
    DefaultMutableTreeNode state = new DefaultMutableTreeNode("California");
    country.add(state);
    DefaultMutableTreeNode city = new DefaultMutableTreeNode("San Jose");
    state.add(city);
    city = new DefaultMutableTreeNode("Cupertino");
    state.add(city);
    state = new DefaultMutableTreeNode("Michigan");
    country.add(state);
    city = new DefaultMutableTreeNode("Ann Arbor");
    state.add(city);
    country = new DefaultMutableTreeNode("Germany");
    root.add(country);
    state = new DefaultMutableTreeNode("Schleswig-Holstein");
    country.add(state);
    city = new DefaultMutableTreeNode("Kiel");
    state.add(city);
    return new JTree(root);
}

From source file:net.landora.animeinfo.notifications.NotificationViewer.java

public void loadNotifications() {
    List<AnimeNotification> notifications = AnimeDBA.getOutstandAnimeNotifications();

    Set<AnimeStub> animes = new HashSet<AnimeStub>();
    for (AnimeNotification notification : notifications) {
        animes.add(notification.getFile().getEpisode().getAnime());
    }//  w  w w  . ja  va  2  s.c  om

    List<AnimeStub> sortedAnime = new ArrayList<AnimeStub>(animes);
    Collections.sort(sortedAnime, UIUtils.LEXICAL_SORTER);

    notificationsNode.removeAllChildren();

    for (AnimeStub anime : sortedAnime) {
        DefaultMutableTreeNode animeNode = new DefaultMutableTreeNode(anime);

        MultiValueMap map = new MultiValueMap();
        for (AnimeNotification notificaton : notifications) {
            if (notificaton.getFile().getEpisode().getAnime().equals(anime)) {
                map.put(notificaton.getFile().getEpisode(), notificaton);
            }
        }
        List<AnimeEpisode> episodes = new ArrayList<AnimeEpisode>(map.keySet());
        Collections.sort(episodes, UIUtils.LEXICAL_SORTER);

        for (AnimeEpisode episode : episodes) {
            DefaultMutableTreeNode episodeNode = new DefaultMutableTreeNode(episode);
            List<AnimeNotification> list = (List<AnimeNotification>) map.get(episode);
            Collections.sort(list, UIUtils.LEXICAL_SORTER);
            for (AnimeNotification notification : list) {
                episodeNode.add(new DefaultMutableTreeNode(notification, false));
            }
            animeNode.add(episodeNode);
        }
        notificationsNode.add(animeNode);
    }

    treeModel.nodeStructureChanged(notificationsNode);
}