title: Configuring a TreeView which scans Local file system to only include folders which have a file type
body: <p>Okay so I used the second block of code from this website 
  <a href="http://www.java2s.com/Tutorials/Java/JavaFX/0660__JavaFX_Tree_View.htm" rel="nofollow noreferrer">http://www.java2s.com/Tutorials/Java/JavaFX/0660__JavaFX_Tree_View.htm</a> where it states "The following code create a dynamic tree from the local file system"</p>
  
  <p>I do not understand how this code works in order to customise it to my needs. Particularly the overriding methods, there did not seem to be a place where I could add in "only add folders down to the sub directory which contain mp3 files". I reckon it's likely going to require something more complex such as something that goes through and removes folders. I'm honestly not sure.</p>
  
  <p>I have tried to use this code in my program which is to show mp3 files. The idea was to have two treeViews side by side, the left side shows the hierarchy of folders to the folders which have mp3 files in them (and do not shows other folders which do not have mp3 files in them) and the right side shows files which are of only mp3 file type in those folders. There is a screenshot further down.</p>
  
  <p>This is the code I have so far which returns a TreeView in a VBox.
  There are two segments of code which are commented out. The first is due to the fact that <a href="https://stackoverflow.com/questions/6251762/java-search-file-according-to-its-name-in-directory-and-subdirectories">java: search file according to its name in directory and subdirectories</a> does not wish to search my C: drive. (I do not know why). So I changed it to only scan my D: (Partition drive). The second is from the webpage where I got the main segment of code. This code was moved to an outer class which handles. As well as a cheeky bit of code to handle more than one drive.</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;
  import javafx.collections.FXCollections;
  import javafx.collections.ObservableList;
  import javafx.scene.control.TreeItem;
  import javafx.scene.control.TreeView;
  import javafx.scene.layout.VBox;
  import org.apache.commons.io.FileUtils;
  
  /**
   * https://stackoverflow.com/questions/6251762/java-search-file-according-to-its-name-in-directory-and-subdirectories
   * https://stackoverflow.com/questions/26690247/how-to-make-directories-expandable-in-javafx-treeview
   * http://www.java2s.com/Tutorials/Java/JavaFX/0660__JavaFX_Tree_View.htm
   *
   * @author Scorchgid
   */
  public class FolderTreeView {
  
      int x = 0;
      private final String fileName = ".mp3";
      private MainView mainView;
      private TreeView&lt;File&gt; treeViewFile = new TreeView&lt;&gt;();
  
      public TreeView&lt;File&gt; getTreeViewFile() {
          return treeViewFile;
      }
  
      public void setTreeViewFile(TreeView&lt;File&gt; treeViewFile) {
          this.treeViewFile = treeViewFile;
      }
  
      public VBox treeStack() throws IOException {
          VBox vbox = new VBox();
          File[] drives = File.listRoots();
          ArrayList&lt;File&gt; fileListing;
          /*for (File dir : drives) {
              System.out.println(dir.toString());
              fileListing = restrictingList(dir);
          }*/
          fileListing = restrictingList(new File("D:\\"));
  
          ArrayList&lt;TreeItem&gt; treeItems = new ArrayList&lt;&gt;();
          for (File dir : drives) {
              //System.out.println(dir.toString());
              treeItems.add(createNode(dir));
          }
          TreeView&lt;File&gt; tree = proxyCreateNode(treeItems);
          vbox.getChildren().add(tree);
  
          return vbox;
      }
  
      // https://stackoverflow.com/questions/22260032/set-two-root-nodes-for-treeview
      public TreeView&lt;File&gt; proxyCreateNode(ArrayList&lt;TreeItem&gt; arrayListTreeItem) {
          TreeItem&lt;File&gt; proxyItem = new TreeItem&lt;&gt;();
          proxyItem.setExpanded(true);
          for (TreeItem&lt;File&gt; item : arrayListTreeItem) {
              proxyItem.getChildren().addAll(item);
          }
          TreeView&lt;File&gt; tree = new TreeView&lt;&gt;(proxyItem);
          tree.setShowRoot(false);
          return tree;
      }
  
      private ArrayList&lt;File&gt; restrictingList(File root) {
          ArrayList&lt;File&gt; fileArray = new ArrayList&lt;&gt;();        
          boolean recursive = true;        
          Collection files = FileUtils.listFiles(root, null, recursive);
          for (Iterator iterator = files.iterator(); iterator.hasNext();) {
              File file = (File) iterator.next();
              if (file.getName().endsWith(fileName)) {
                  fileArray.add(file);              
              }
          }
          return fileArray;
      }
  
      /*    @Override
       public void start(Stage stage) {
       Scene scene = new Scene(new Group(), 300, 300);
  
       TreeItem&lt;File&gt; root = createNode(new File("c:/"));
       TreeView treeView = new TreeView&lt;File&gt;(root);
  
       vbox.getChildren().add(treeView);
       ((Group) scene.getRoot()).getChildren().add(vbox);
  
       stage.setScene(scene);
       stage.show();
       }
       */
      private TreeItem&lt;File&gt; createNode(final File f) {
          return new TreeItem&lt;File&gt;(f) {
              private boolean isLeaf;
              private boolean isFirstTimeChildren = true;
              private boolean isFirstTimeLeaf = true;
  
              @Override
              public ObservableList&lt;TreeItem&lt;File&gt;&gt; getChildren() {
                  if (isFirstTimeChildren) {
                      isFirstTimeChildren = false;
                      super.getChildren().setAll(buildChildren(this));
                  }
                  return super.getChildren();
              }
  
              @Override
              public boolean isLeaf() {
                  if (isFirstTimeLeaf) {
                      isFirstTimeLeaf = false;
                      File f = (File) getValue();
                      isLeaf = f.isFile();
                  }
                  return isLeaf;
              }
  
              private ObservableList&lt;TreeItem&lt;File&gt;&gt; buildChildren(
                      TreeItem&lt;File&gt; TreeItem) {
                  File f = TreeItem.getValue();
                  if (f == null) {
                      return FXCollections.emptyObservableList();
                  }
                  if (f.isFile()) {
                      return FXCollections.emptyObservableList();
                  }
                  File[] files = f.listFiles();
                  if (files != null) {
                      ObservableList&lt;TreeItem&lt;File&gt;&gt; children = FXCollections
                              .observableArrayList();
                      for (File childFile : files) {
                          //System.out.println("Adding " + childFile.getAbsolutePath());
                          if (childFile.isDirectory()) {
                              children.add(createNode(childFile));
                          }
                      }
                      return children;
                  }
                  return FXCollections.emptyObservableList();
              }
          };
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/MsB9O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MsB9O.png" alt="A preview of a portion of the GUI, the TreeView on the left is from the class shown above "></a></p>