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

DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(dir);
    if (curTop != null) {
        curTop.add(curDir);
    }//from  w  w  w .j  a  va 2s . c  om
    File[] tmp = dir.listFiles();
    Vector<File> ol = new Vector<File>();
    ol.addAll(Arrays.asList(tmp));
    Collections.sort(ol, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {
            int result = o1.getName().compareTo(o2.getName());
            if (o1.isDirectory() && o2.isFile()) {
                result = -1;
            } else if (o2.isDirectory() && o1.isFile()) {
                result = 1;
            }
            return result;
        }
    });
    for (int i = 0; i < ol.size(); i++) {
        File file = ol.elementAt(i);
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(file);
        if (file.isDirectory()) {
            addNodes(node, file);
        }
        curDir.add(node);
    }
    return curDir;
}

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 w w w.  j  a  v a  2 s . co  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:i18nplugin.TranslationNode.java

/**
 * Add a Property-File/*from   w  w w. j  a  va  2 s.co m*/
 * @param jarfile Main-Jar
 * @param entry Property-File
 */
private void addEntry(JarFile jarfile, JarEntry entry) {
    String name = entry.getName();
    DefaultMutableTreeNode path = getPath(name.substring(0, name.lastIndexOf('/')));
    path.add(new PropertiesNode(jarfile, entry));
}

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);
    }// w  w w .j  a v  a2  s  .  c om
    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 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 a v a 2s .  com
    Vector<String> ol = new Vector<String>();
    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<Object> files = new Vector<Object>();
    for (int i = 0; i < ol.size(); i++) {
        String thisObject = 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);
        }
    }
    for (int fnum = 0; fnum < files.size(); fnum++) {
        curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
    }
    return curDir;
}

From source file:EditableTree.java

public EditableTree() {
    DefaultMutableTreeNode forums = new DefaultMutableTreeNode("A");
    forums.add(new DefaultMutableTreeNode("B"));
    DefaultMutableTreeNode articles = new DefaultMutableTreeNode("E");
    articles.add(new DefaultMutableTreeNode("F"));
    DefaultMutableTreeNode examples = new DefaultMutableTreeNode("G");
    examples.add(new DefaultMutableTreeNode("H"));

    m_rootNode.add(forums);// ww w  .  j  a  va 2 s.c  o  m
    m_rootNode.add(articles);
    m_rootNode.add(examples);

    m_tree.setEditable(true);
    m_tree.setSelectionRow(0);

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

    JPanel panel = new JPanel();

    m_addButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();
            if (selNode == null) {
                return;
            }
            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node");
            m_model.insertNodeInto(newNode, selNode, selNode.getChildCount());

            TreeNode[] nodes = m_model.getPathToRoot(newNode);
            TreePath path = new TreePath(nodes);
            m_tree.scrollPathToVisible(path);
            m_tree.setSelectionPath(path);
            m_tree.startEditingAtPath(path);
        }
    });
    panel.add(m_addButton);

    m_delButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent();
            removeNode(selNode);
        }
    });
    panel.add(m_delButton);

    JPanel searchPanel = new JPanel();
    searchPanel.setBorder(BorderFactory.createEtchedBorder());

    m_searchText = new JTextField(10);
    searchPanel.add(m_searchText);

    m_searchButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode node = searchNode(m_searchText.getText());
            if (node != null) {
                TreeNode[] nodes = m_model.getPathToRoot(node);
                TreePath path = new TreePath(nodes);
                m_tree.scrollPathToVisible(path);
                m_tree.setSelectionPath(path);
            } else {
                System.out.println("Node with string " + m_searchText.getText() + " not found");
            }
        }
    });
    searchPanel.add(m_searchButton);

    m_searchAndDeleteButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            DefaultMutableTreeNode node = searchNode(m_searchText.getText());
            if (node != null) {
                removeNode(node);
            } else {
                System.out.println("Node with string " + m_searchText.getText() + " not found");
            }
        }
    });
    searchPanel.add(m_searchAndDeleteButton);
    panel.add(searchPanel);
    getContentPane().add(panel, BorderLayout.SOUTH);
    setSize(700, 400);
    setVisible(true);
}

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 ww  .  jav  a  2s .co m
}

From source file:Main.java

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

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

    book = new DefaultMutableTreeNode(new BookInfo("Java Tutorial"));
    category.add(book);/* w  w w.  j a  v a  2s . c o m*/
    book = new DefaultMutableTreeNode(new BookInfo("JDK"));
    category.add(book);
    book = new DefaultMutableTreeNode(new BookInfo("GUI"));
    category.add(book);
    book = new DefaultMutableTreeNode(new BookInfo("Guide"));
    category.add(book);

    book = new DefaultMutableTreeNode(new BookInfo("Language"));
    category.add(book);

    book = new DefaultMutableTreeNode(new BookInfo("SQL"));
    category.add(book);

    category = new DefaultMutableTreeNode("CSS");
    top.add(category);

    book = new DefaultMutableTreeNode(new BookInfo("Spec"));
    category.add(book);

    book = new DefaultMutableTreeNode(new BookInfo("Javascript"));
    category.add(book);
}

From source file:TreeEditTest.java

public TreeNode makeSampleTree() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("World");
    DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA");
    root.add(country);
    DefaultMutableTreeNode state = new DefaultMutableTreeNode("California");
    country.add(state);//w  ww .  j a  va2  s .com
    DefaultMutableTreeNode city = new DefaultMutableTreeNode("San Jose");
    state.add(city);
    city = new DefaultMutableTreeNode("San Diego");
    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 root;
}

From source file:Tree1.java

public Tree1() {
    super("Sample Tree [OID]");
    setSize(400, 300);/*  w w w.  j av  a  2  s  .c o  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);
}