JTree add nodes on startup of application
Asked Answered
F

2

4

I want to make text editor with file browser so when I start my application I want to my program add nodes on JTree so it shows me all files and folders for example in My Documents folder, and to give me ability to access to those files and folders (especially to folders). I tried to figure out how Andrew Thompson did that from this example but I failed. I managed to create nodes for all files and folders from My Documents using this example . But thats all, I can't figure out how to generate nodes for other files and folders when clicking on one of nodes which represent folder.

This is what I've done till now:

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;


public class MyTextEditor extends JFrame{

    JTree tree;
    JTabbedPane tabbedPane = new JTabbedPane();
    File myDocumentsFolder = new File("C:/Documents and Settings/User/My Documents");
    File[] listOfFiles = myDocumentsFolder.listFiles();
    String dirTitle = myDocumentsFolder.getName();
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(dirTitle);
    DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);

    public MyTextEditor() {

        tree = new JTree(treeModel);
        tree.setEditable(false);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.setShowsRootHandles(true);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(tree),tabbedPane);
        add(splitPane);

        tree.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){
                for (int i = 0; i < listOfFiles.length; i++) {
                    String nameOfFile = listOfFiles[i].getName();
                    rootNode.add(new DefaultMutableTreeNode(nameOfFile));
                }
            }
        });

    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                MyTextEditor mte = new MyTextEditor();
                mte.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mte.setPreferredSize(new Dimension(800,600));
                mte.pack();
                mte.setLocationByPlatform(true);
                mte.setVisible(true);
            }
        });
    }

}

Can someone tell me how to generate nodes for all files and folders for specific folder. Thanks in advance.

Fogle answered 6/11, 2012 at 19:39 Comment(1)
"I tried to figure out how Andrew Thompson did that from this example but I failed." Huhh.. I have a vague feeling I failed if it was not 'intuitively obvious' (at least to someone like you, who I am used to up-voting). Good question. Hope you get a good answer. If not, let me know and I'll delve into your code.Cubiculum
W
4

I use this FileTreeModel for the TreeModel, Outline for the view, and user.dir for the starting directory.

TreeModel treeModel = new FileTreeModel(
    new File(System.getProperty("user.dir")));
OutlineModel outlineModel = DefaultOutlineModel.createOutlineModel(
    treeModel, new FileRowModel(), true, "User Directory");
Woodson answered 6/11, 2012 at 20:12 Comment(0)
A
3

Make a recursive function that takes in the root node, adds nodes for each file/dir underneath it, and then calls itself again on each of those nodes.

Edit: no need to inherit from DefaultMutableTreeNode if each node already contains the relative path.

Amid answered 6/11, 2012 at 19:52 Comment(1)
I appreciate your answer. I will try that way.Fogle

© 2022 - 2024 — McMap. All rights reserved.