This is an old revision of the document!


JTree
Populating Tree
public class TestPane extends JPanel {
 
        private DefaultTreeModel model;
        private JTree tree;
 
        public TestPane() {
            setLayout(new BorderLayout());
 
            tree = new JTree();
            File rootFile = new File(".");
            DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootFile);
            model = new DefaultTreeModel(root);
 
            tree.setModel(model);
            tree.setRootVisible(true);
            tree.setShowsRootHandles(true);
 
            add(new JScrollPane(tree));
 
            JButton load = new JButton("Load");
            add(load, BorderLayout.SOUTH);
 
            load.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
 
                    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
                    root.removeAllChildren();
                    model.reload();
                    File rootFile = (File) root.getUserObject();
 
                    addFiles(rootFile, model, root);
 
                    tree.expandPath(new TreePath(root));
 
                }
            });
 
        }
 
        protected void addFiles(File rootFile, DefaultTreeModel model, DefaultMutableTreeNode root) {
 
            for (File file : rootFile.listFiles()) {
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(file);
                model.insertNodeInto(child, root, root.getChildCount());
                if (file.isDirectory()) {
                    addFiles(file, model, child);
                }
            }
 
        }
}        

Reference: https://stackoverflow.com/questions/15215578/how-to-build-a-jtree-dynamically

File List in Tree
package com.ggl.testing;
 
import java.io.File;
 
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
 
public class FileBrowser implements Runnable {
 
    private DefaultMutableTreeNode root;
 
    private DefaultTreeModel treeModel;
 
    private JTree tree;
 
    @Override
    public void run() {
        JFrame frame = new JFrame("File Browser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        File fileRoot = new File("C:/");
        root = new DefaultMutableTreeNode(new FileNode(fileRoot));
        treeModel = new DefaultTreeModel(root);
 
        tree = new JTree(treeModel);
        tree.setShowsRootHandles(true);
        JScrollPane scrollPane = new JScrollPane(tree);
 
        frame.add(scrollPane);
        frame.setLocationByPlatform(true);
        frame.setSize(640, 480);
        frame.setVisible(true);
 
        CreateChildNodes ccn = 
                new CreateChildNodes(fileRoot, root);
        new Thread(ccn).start();
    }
 
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new FileBrowser());
    }
 
    public class CreateChildNodes implements Runnable {
 
        private DefaultMutableTreeNode root;
 
        private File fileRoot;
 
        public CreateChildNodes(File fileRoot, 
                DefaultMutableTreeNode root) {
            this.fileRoot = fileRoot;
            this.root = root;
        }
 
        @Override
        public void run() {
            createChildren(fileRoot, root);
        }
 
        private void createChildren(File fileRoot, 
                DefaultMutableTreeNode node) {
            File[] files = fileRoot.listFiles();
            if (files == null) return;
 
            for (File file : files) {
                DefaultMutableTreeNode childNode = 
                        new DefaultMutableTreeNode(new FileNode(file));
                node.add(childNode);
                if (file.isDirectory()) {
                    createChildren(file, childNode);
                }
            }
        }
 
    }
 
    public class FileNode {
 
        private File file;
 
        public FileNode(File file) {
            this.file = file;
        }
 
        @Override
        public String toString() {
            String name = file.getName();
            if (name.equals("")) {
                return file.getAbsolutePath();
            } else {
                return name;
            }
        }
    }
 
}

References: https://stackoverflow.com/questions/23804675/list-files-and-directories-with-jtree-and-file-in-java

File List in JTree
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.DefaultListModel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
//...
 
public class frmMain extends javax.swing.JFrame 
{
    public frmMain() 
    {
        initComponents();
 
        LaunchWebView();
        OpenXmlDataFile();
 
        try {
             treeFileList.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("C:\\TEMP")));
             scanDirectory("C:\\TEMP", treeFileList);
        } catch (Exception exc) {
           // Do something
        }
    }
 
    public void scanDirectory(String location, JTree tree) throws InterruptedException 
    {    
        // Ccreates a file with the location filename
        //String location ="C:\\Users\\Ashish Padalkar\\Documents";
        File currentDir = new File(location);
 
        // Result is the variable name for jtree
        DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
        // Gets the root of the current model used only once at the starting
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
 
        displayDirectoryContents(currentDir, tree, root);
    }
 
    private void displayDirectoryContents(File dir, JTree tree, DefaultMutableTreeNode treeRoot) throws InterruptedException 
    {   
        File[] files = dir.listFiles();  // Creates array of file type for all the files found
 
        for (File file : files) {
            if(file == null) {
                System.out.println("NULL directory found");
                continue;
            }
            if (file.isDirectory()) {
                // Folder
               if (file.listFiles() == null) {
                    // Skip null files
                    continue;
                }
 
                // Adds a node to the root of the JTree
                DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
                DefaultMutableTreeNode newdir = new DefaultMutableTreeNode(file.getName());
                treeRoot.add(newdir);
 
                // Refresh the model to show the changes
                model.reload();
 
                // Recursively calls the function again to explore the contents folder
                displayDirectoryContents(file, tree, newdir);
 
            } else {  // File is not a directory
 
                // Gets the current model of the tree
                DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
 
                // Selected node is the position where the new node will be inserted
                DefaultMutableTreeNode selectednode = treeRoot;
                DefaultMutableTreeNode newfile = new DefaultMutableTreeNode(file.getName());
 
                // Inserts a node newfile under selected node which is the root
                model.insertNodeInto(newfile, selectednode, selectednode.getChildCount());
 
                // Refresh the model to show the changes
                model.reload();
            }
        }    
    }