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/08 10:42]
smayr [File List in JTree]
swdev:java:component:jtree [2017/10/08 11:25] (current)
smayr [File List in JTree]
Line 189: Line 189:
                  
         try {         try {
-             treeFileList.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("C:\\TEMP"))); +             treeFileList.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("C:\\TEMP")));   // clear existing model 
-             scanDirectory("C:\\TEMP", treeFileList);+             FileUtils.scanAndLoadDirectory("C:\\TEMP", treeFileList);
         } catch (Exception exc) {         } catch (Exception exc) {
            // Do something            // Do something
Line 196: Line 196:
     }     }
  
-    public void scanDirectory(String location, JTree tree) throws InterruptedException +}     
 +</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 
     {         {    
-        // Ccreates a file with the location filename+        // Creates a file with the location filename
         //String location ="C:\\Users\\Ashish Padalkar\\Documents";         //String location ="C:\\Users\\Ashish Padalkar\\Documents";
         File currentDir = new File(location);         File currentDir = new File(location);
  
-        // Result is the variable name for jtree +        // Get root of tree model 
-        DefaultTreeModel model = (DefaultTreeModel)tree.getModel(); +        DefaultTreeModel modelTree = (DefaultTreeModel)tree.getModel(); 
-        // Gets the root of the current model used only once at the starting +        DefaultMutableTreeNode root = (DefaultMutableTreeNode) modelTree.getRoot();
-        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();+
                  
-        displayDirectoryContents(currentDir, tree, root);+        displayDirectoryContents(currentDir, modelTree, root);
     }     }
          
-    private void displayDirectoryContents(File dir, JTree tree, DefaultMutableTreeNode treeRoot) throws InterruptedException +    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         File[] files = dir.listFiles();  // Creates array of file type for all the files found
  
         for (File file : files) {         for (File file : files) {
-            if(file == null) {+            if (file == null) {
                 System.out.println("NULL directory found");                 System.out.println("NULL directory found");
                 continue;                 continue;
             }             }
             if (file.isDirectory()) {             if (file.isDirectory()) {
-                // Folder+                //--------------------------- 
 +                // File is a Directory 
 +                //---------------------------
                if (file.listFiles() == null) {                if (file.listFiles() == null) {
                     // Skip null files                     // Skip null files
Line 226: Line 240:
                 }                 }
  
-                // Adds a node to the root of the JTree +                // Adds a new Directory node to tree root
-                DefaultTreeModel model = (DefaultTreeModel)tree.getModel();+
                 DefaultMutableTreeNode newdir = new DefaultMutableTreeNode(file.getName());                 DefaultMutableTreeNode newdir = new DefaultMutableTreeNode(file.getName());
-                treeRoot.add(newdir);+                curRoot.add(newdir);
  
-                // Refresh the model to show the changes+                // Refresh the model to show changes
                 model.reload();                 model.reload();
  
                 // Recursively calls the function again to explore the contents folder                 // Recursively calls the function again to explore the contents folder
-                displayDirectoryContents(file, tree, newdir);+                displayDirectoryContents(file, model, newdir);
                                  
-            } else {  // File is not a directory +            } else {   
- +                //--------------------------- 
-                // Gets the current model of the tree +                // File is not a directory 
-                DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); +                //--------------------------- 
- +                // Inserts a node newfile under selected node which is the root 
-                // Selected node is the position where the new node will be inserted +                DefaultMutableTreeNode selectednode = curRoot;
-                DefaultMutableTreeNode selectednode = treeRoot;+
                 DefaultMutableTreeNode newfile = new DefaultMutableTreeNode(file.getName());                 DefaultMutableTreeNode newfile = new DefaultMutableTreeNode(file.getName());
- 
-                // Inserts a node newfile under selected node which is the root 
                 model.insertNodeInto(newfile, selectednode, selectednode.getChildCount());                 model.insertNodeInto(newfile, selectednode, selectednode.getChildCount());
  
Line 254: Line 264:
         }             }    
     }     }
-</code>        +
 +</code>