Example usage for javax.swing.tree DefaultTreeModel DefaultTreeModel

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

Introduction

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

Prototype

@ConstructorProperties({ "root" })
public DefaultTreeModel(TreeNode root) 

Source Link

Document

Creates a tree in which any node can have children.

Usage

From source file:FileTree2.java

public FileTree2() {
    super("Directories Tree [Popup Menus]");
    setSize(400, 300);// w  w  w. jav  a 2 s  .c o  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);
    m_tree = new JTree(m_model);

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

    // NEW
    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(FileTree2.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(FileTree2.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:FileTree3.java

public FileTree3() {
    super("Directories Tree [Tool Tips]");
    setSize(400, 300);//from  w w  w.j  ava  2 s  . c  om

    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:de.quadrillenschule.azocamsyncd.gui.ExploreWifiSDPanel.java

/**
 * Creates new form ExploreWifiSDPanel//from   w  w w. java  2s .c  om
 */
public ExploreWifiSDPanel() {
    initComponents();
    rootNode = new DefaultMutableTreeNode("/");
    DefaultTreeModel dtm = new DefaultTreeModel(rootNode);
    remotejTree.setModel(dtm);
    remotejTree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            updateSingleView();
        }
    });
    remotejTree.setCellRenderer(new TreeCellRenderer() {

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                boolean expanded, boolean leaf, int row, boolean hasFocus) {
            JLabel retval = new JLabel(value.toString());
            AZoFTPFile myaffile = null;
            if (afs == null) {
                return retval;
            }
            for (AZoFTPFile af : afs) {
                if (new String(af.dir + af.ftpFile.getName()).equals(value.toString())) {
                    myaffile = af;
                    break;
                }
            }
            try {
                if (!myaffile.ftpFile.isDirectory()) {
                    if (!localStorage.getLocalFile(myaffile).exists()) {

                        if (!localStorage.isFileSynced(myaffile)) {
                            retval.setForeground(new Color(0, 20, 100));
                        }
                    } else {
                        retval.setForeground(new Color(20, 100, 0));
                    }
                }
            } catch (Exception ex) {
                //           Logger.getLogger(ExploreWifiSDPanel.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (selected) {
                retval.setOpaque(true);
                retval.setBackground(Color.darkGray);
            }
            return retval;
        }
    });
}

From source file:TreeEditTest.java

public TreeEditFrame() {
    setTitle("TreeEditTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // construct tree

    TreeNode root = makeSampleTree();
    model = new DefaultTreeModel(root);
    tree = new JTree(model);
    tree.setEditable(true);//  w  w  w .  jav a 2s. c  o  m

    // add scroll pane with tree

    JScrollPane scrollPane = new JScrollPane(tree);
    add(scrollPane, BorderLayout.CENTER);

    makeButtons();
}

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

public TreeComponent(MessageSourceAccessor messageSourceAccesor, String name, String jsonString) {
    super();//from  w  w w .  java2s.  com
    this.msa = messageSourceAccesor;

    final String localizedName = msa.getMessage(name);

    if (StringUtils.isNotBlank(jsonString)) {
        buildTree(localizedName, JsonUtils.deserialize(jsonString));
    } else {
        final DefaultMutableTreeNode rootTreeNode = createSingleNode(localizedName, true, true);
        model = new DefaultTreeModel(rootTreeNode);
    }
}

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

/**
 * Creates new form NotificationViewer/*from   w w w.j  ava  2  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);
}

From source file:Main.java

public void setPath(String filePath) {
    Node root = null;//w ww .  java  2 s  . c  om
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(filePath);
        root = (Node) doc.getDocumentElement();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Can't parse file", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    }
    if (root != null) {
        dtModel = new DefaultTreeModel(builtTreeNode(root));
        this.setModel(dtModel);
    }
}

From source file:com.mindcognition.mindraider.ui.swing.explorer.NotebooksTree.java

public NotebooksTree() {
    // TODO bundle
    notebookUris = new HashSet<String>();
    notebooksRootNode = new DefaultMutableTreeNode(new NotebookNodeUserObject("Outlines", 0, null));

    notebooksTreeModel = new DefaultTreeModel(notebooksRootNode);
    setModel(notebooksTreeModel);/*from  w ww .  j av  a 2s  .com*/

    setEditable(false);
    getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    addTreeExpansionListener(new TreeExpansionListenerImplementation());
    addTreeWillExpandListener(new TreeWillExpandListenerImplementation());
    setShowsRootHandles(true);

    setCellRenderer(new OutlinesTreeCellRenderer());

    // tree node selection listener
    addTreeSelectionListener(new NotebooksTreeSelectionListener(this));

    reloadModel();
}

From source file:Tree1.java

public Tree1() {
    super("Sample Tree [OID]");
    setSize(400, 300);//from  www.  j  a v  a  2  s . co  m

    Object[] nodes = new Object[5];
    DefaultMutableTreeNode top = new DefaultMutableTreeNode(new OidNode(1, "ISO"));
    DefaultMutableTreeNode parent = top;
    nodes[0] = top;

    DefaultMutableTreeNode node = new DefaultMutableTreeNode(new OidNode(0, "standard"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(2, "member-body"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(3, "org"));
    parent.add(node);
    parent = node;
    nodes[1] = parent;

    node = new DefaultMutableTreeNode(new OidNode(6, "dod"));
    parent.add(node);
    parent = node;
    nodes[2] = parent;

    node = new DefaultMutableTreeNode(new OidNode(1, "internet"));
    parent.add(node);
    parent = node;
    nodes[3] = parent;

    node = new DefaultMutableTreeNode(new OidNode(1, "directory"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(2, "mgmt"));
    parent.add(node);
    nodes[4] = node;
    node.add(new DefaultMutableTreeNode(new OidNode(1, "mib-2")));
    node = new DefaultMutableTreeNode(new OidNode(3, "experimental"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(4, "private"));
    node.add(new DefaultMutableTreeNode(new OidNode(1, "enterprises")));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(5, "security"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(6, "snmpV2"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(7, "mail"));
    parent.add(node);

    m_model = new DefaultTreeModel(top);
    m_tree = new JTree(m_model);

    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
    renderer.setOpenIcon(new ImageIcon("opened.gif"));
    renderer.setClosedIcon(new ImageIcon("closed.gif"));
    renderer.setLeafIcon(new ImageIcon("leaf.gif"));
    m_tree.setCellRenderer(renderer);

    m_tree.setShowsRootHandles(true);
    m_tree.setEditable(false);
    TreePath path = new TreePath(nodes);
    m_tree.setSelectionPath(path);

    m_tree.addTreeSelectionListener(new OidSelectionListener());

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

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}

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 {//ww w .  jav a  2 s.  c  o m
            rootTreeNode.add(createChildNodes(trace));
        }
    }
}