Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:java:component:jtree [2017/10/02 08:54]
smayr
swdev:java:component:jtree [2017/10/08 11:25] (current)
smayr [File List in JTree]
Line 165: Line 165:
  
 References: [[https://stackoverflow.com/questions/23804675/list-files-and-directories-with-jtree-and-file-in-java]] References: [[https://stackoverflow.com/questions/23804675/list-files-and-directories-with-jtree-and-file-in-java]]
 +
 +== File List in JTree ==
 +<code java>
 +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")));   // clear existing model
 +             FileUtils.scanAndLoadDirectory("C:\\TEMP", treeFileList);
 +        } catch (Exception exc) {
 +           // Do something
 +        }
 +    }
 +
 +}    
 +</code>        
 +
 +<code java>
 +package com.acme.myapp;
 +
 +import java.io.File;
 +import javax.swing.JTree;
 +import javax.swing.tree.DefaultMutableTreeNode;
 +import javax.swing.tree.DefaultTreeModel;
 +
 +public class FileUtils 
 +{
 +    public static void scanAndLoadDirectory(String location, JTree tree) throws InterruptedException 
 +    {    
 +        // Creates a file with the location filename
 +        //String location ="C:\\Users\\Ashish Padalkar\\Documents";
 +        File currentDir = new File(location);
 +
 +        // Get root of tree model
 +        DefaultTreeModel modelTree = (DefaultTreeModel)tree.getModel();
 +        DefaultMutableTreeNode root = (DefaultMutableTreeNode) modelTree.getRoot();
 +        
 +        displayDirectoryContents(currentDir, modelTree, root);
 +    }
 +    
 +    public static void displayDirectoryContents(File dir, DefaultTreeModel model, DefaultMutableTreeNode curRoot) 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()) {
 +                //---------------------------
 +                // File is a Directory
 +                //---------------------------
 +               if (file.listFiles() == null) {
 +                    // Skip null files
 +                    continue;
 +                }
 +
 +                // Adds a new Directory node to tree root
 +                DefaultMutableTreeNode newdir = new DefaultMutableTreeNode(file.getName());
 +                curRoot.add(newdir);
 +
 +                // Refresh the model to show changes
 +                model.reload();
 +
 +                // Recursively calls the function again to explore the contents folder
 +                displayDirectoryContents(file, model, newdir);
 +                
 +            } else {  
 +                //---------------------------
 +                // File is not a directory
 +                //---------------------------
 +                // Inserts a node newfile under selected node which is the root
 +                DefaultMutableTreeNode selectednode = curRoot;
 +                DefaultMutableTreeNode newfile = new DefaultMutableTreeNode(file.getName());
 +                model.insertNodeInto(newfile, selectednode, selectednode.getChildCount());
 +
 +                // Refresh the model to show the changes
 +                model.reload();
 +            }
 +        }    
 +    }
 +}
 +</code>