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

DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
    String curPath = dir.getPath();
    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
    if (curTop != null) {
        curTop.add(curDir);/*from  ww w . j a  va 2 s  .c o m*/
    }

    List<File> files = new ArrayList<File>(Arrays.asList(dir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            String name = pathname.getName().toLowerCase();
            return name.endsWith(".h")
                    || (pathname.isDirectory() && !("System Volume Information".equalsIgnoreCase(name)));
        }
    })));

    Collections.sort(files);

    for (File file : files) {
        if (file.isDirectory()) {
            addNodes(curDir, file);
        }
    }
    for (File file : files) {
        if (file.isFile()) {
            curDir.add(new DefaultMutableTreeNode(file));
        }
    }
    return curDir;
}

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);/*  www  .  j  av a  2  s . c  o m*/
    }
}

From source file:Main.java

public Main() {
    setLayout(new BorderLayout());
    treeModel = new DefaultTreeModel(root);
    tree = new JTree(treeModel);
    treeScroll = new JScrollPane(tree);
    add(treeScroll, BorderLayout.WEST);

    properties.setProperty("foo1", "bar1");
    properties.setProperty("foo2", "bar2");
    properties.setProperty("foo3", "bar3");
    properties.setProperty("foo4", "bar4");

    Set<Object> keySet = properties.keySet();
    for (Object key : keySet) {
        root.add(new DefaultMutableTreeNode(key));
    }// w  w  w  .  j  a v a 2 s  .  c  o m

    tree.expandPath(new TreePath(root));
    descriptionLabel = new JLabel(NOTHING_SELECTED);
    add(descriptionLabel, BorderLayout.CENTER);

    tree.addTreeSelectionListener(e -> {
        DefaultMutableTreeNode selection = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (selection != null) {
            String key = (String) selection.getUserObject();
            String command = properties.getProperty(key);
            descriptionLabel.setText(command);
        } else {
            descriptionLabel.setText(NOTHING_SELECTED);
        }
    });
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
}

From source file:VSX.java

public TreeModel parse(String filename) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    XMLTreeHandler handler = new XMLTreeHandler();
    try {//from w  w w .j a v  a2 s .co m
        // Parse the input.
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(new File(filename), handler);
    } catch (Exception e) {
        System.err.println("File Read Error: " + e);
        e.printStackTrace();
        return new DefaultTreeModel(new DefaultMutableTreeNode("error"));
    }
    return new DefaultTreeModel(handler.getRoot());
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TreeNode root = getNodes();//from  w w  w. j  a  v a2 s .c o  m
    DefaultTreeModel model = new DefaultTreeModel(root);
    JTree tree = new JTree(model);
    tree.setRootVisible(false);

    JButton add = new JButton("add new");
    add.addActionListener(e -> {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (selectedNode == null) {
            return;
        }
        MyObject obj = (MyObject) selectedNode.getUserObject();
        MyObject newChild = new MyObject(obj.name + "-" + (obj.childs.size() + 1));
        obj.childs.add(newChild);
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(newChild);
        model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount());
        TreeNode[] nodes = model.getPathToRoot(newNode);
        TreePath path = new TreePath(nodes);
        tree.scrollPathToVisible(path);
    });

    JButton print = new JButton("print childs");
    print.addActionListener(e -> {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (selectedNode == null) {
            return;
        }
        MyObject obj = (MyObject) selectedNode.getUserObject();
        System.out.println(obj.childs);
    });
    JPanel btns = new JPanel();
    btns.add(add);
    btns.add(print);

    add(new JScrollPane(tree));
    add(btns, BorderLayout.SOUTH);
    pack();
    setVisible(true);
}

From source file:MainClass.java

DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
    String curPath = dir.getPath();
    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
    if (curTop != null) {
        curTop.add(curDir);//from   w w w .  j  ava  2 s.  co  m
    }
    ArrayList ol = new ArrayList();
    String[] tmp = dir.list();
    for (int i = 0; i < tmp.length; i++)
        ol.add(tmp[i]);
    File f;
    ArrayList files = new ArrayList();

    for (int i = 0; i < ol.size(); i++) {
        String thisObject = (String) ol.get(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.add(thisObject);
    }
    for (int fnum = 0; fnum < files.size(); fnum++)
        curDir.add(new DefaultMutableTreeNode(files.get(fnum)));
    return curDir;
}

From source file:Main.java

private void buildTreeFromString(final DefaultTreeModel model, final String str) {
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    String[] strings = str.split("/");
    DefaultMutableTreeNode node = root;
    for (String s : strings) {
        int index = childIndex(node, s);
        if (index < 0) {
            DefaultMutableTreeNode newChild = new DefaultMutableTreeNode(s);
            node.insert(newChild, node.getChildCount());
            node = newChild;/*w w w.j a  va2s.co m*/
        } else {
            node = (DefaultMutableTreeNode) node.getChildAt(index);
        }
    }
}

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 w w w  . j a  v  a  2 s .  com
            child = new DefaultMutableTreeNode(nodeSpecifier); //  Leaf
        node.add(child);
    }
    return (node);
}

From source file:FileTree2.java

public FileTree2() {
    super("Directories Tree [Popup Menus]");
    setSize(400, 300);/*  w ww . 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);
    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:Main.java

public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    root = new DefaultMutableTreeNode("Root");
    root.add(e1);/*from w  w w  .  ja v a2 s  .  c o  m*/
    root.add(e2);
    root.add(e3);
    e1.add(e11);
    e2.add(e22);
    e3.add(e33);
    e11.add(e111);
    e22.add(e222);
    e33.add(e333);

    tree = new JTree(root);
    tree.addTreeSelectionListener(this);
    add(new JScrollPane(tree), BorderLayout.CENTER);
    currentSelectionField = new JTextField("Current Selection: NONE");
    add(currentSelectionField, BorderLayout.SOUTH);
    setSize(250, 275);
    setLocationRelativeTo(null);
    setVisible(true);
}