2016-07-09 14 views
0

ich einen TreeView erstellen bin versucht, die Verzeichnisinhalte zeigt, wie:JavaFX TreeView Verzeichnisliste

ABC 
    BCD 
    123.php 

Wo ABC und BCD sind beide Verzeichnisse. Ich fühle mich, als ob ich etwas vermisse, da die TreeView gut funktioniert, bevor ich die vollständige Verzeichnisposition ausstrippe, aber sobald ich es ausziehe, wird es nicht wie oben angezeigt.

public void displayTreeView(String inputDirectoryLocation, CheckBoxTreeItem<String> mainRootItem) { 

    // Creates the root item. 
    CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<>(inputDirectoryLocation); 

    // Hides the root item of the tree view. 
    treeView.setShowRoot(false); 

    // Creates the cell factory. 
    treeView.setCellFactory(CheckBoxTreeCell.<String>forTreeView()); 

    // Get a list of files. 
    File fileInputDirectoryLocation = new File(inputDirectoryLocation); 
    File fileList[] = fileInputDirectoryLocation.listFiles(); 

    // Loop through each file and directory in the fileList. 
    for (int i = 0; i < fileList.length; i++) { 

     // Check if fileList[i] is a file or a directory. 
     if (fileList[i].isDirectory()) { 

      // Re-iterate through as is directory. 
      displayTreeView(fileList[i].toString(), rootItem); 

     } else { 

      // Check the file type of the file. 
      String fileType = Util.retrieveFileType(fileList[i].toString()); 

      // Check if the file type is the same file type we are searching for. In the future we just add the or symbol to support other file types. 
      if (fileType.equals(".php")) { 

       // Creates the item. 
       CheckBoxTreeItem<String> fileCheckBoxTreeItem = new CheckBoxTreeItem<>(fileList[i].getName()); 

       // Adds to the treeview. 
       rootItem.getChildren().add(fileCheckBoxTreeItem); 
      } 
     } 
    } 

    // Check if the mainRootItem has been specified. 
    if (mainRootItem == null) { 

     // Sets the tree view root item. 
     treeView.setRoot(rootItem); 
    } else { 

     // Creates the root item. 
     CheckBoxTreeItem<String> dirCheckBoxTreeItem = new CheckBoxTreeItem<>(fileInputDirectoryLocation.getName()); 

     // Sets the sub-root item. 
     mainRootItem.getChildren().add(dirCheckBoxTreeItem); 
    } 
} 

Antwort

2

die Initialisierung des TreeView und eine rekursive Methode zur Konstruktion des Baumes Durch die Kombination Sie unordentlich Code erstellt.

Besser eine neue Methode erstellen, für die Erstellung des Baumes:

public static void createTree(File file, CheckBoxTreeItem<String> parent) { 
    if (file.isDirectory()) { 
     CheckBoxTreeItem<String> treeItem = new CheckBoxTreeItem<>(file.getName()); 
     parent.getChildren().add(treeItem); 
     for (File f : file.listFiles()) { 
      createTree(f, treeItem); 
     } 
    } else if (".php".equals(Util.retrieveFileType(file.toString()))) { 
     parent.getChildren().add(new CheckBoxTreeItem<>(file.getName())); 
    } 
} 

und verwenden Sie es in Ihrer displayTreeView Methode

public void displayTreeView(String inputDirectoryLocation) { 
    // Creates the root item. 
    CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<>(inputDirectoryLocation); 

    // Hides the root item of the tree view. 
    treeView.setShowRoot(false); 

    // Creates the cell factory. 
    treeView.setCellFactory(CheckBoxTreeCell.<String>forTreeView()); 

    // Get a list of files. 
    File fileInputDirectoryLocation = new File(inputDirectoryLocation); 
    File fileList[] = fileInputDirectoryLocation.listFiles(); 

    // create tree 
    for (File file : fileList) { 
     createTree(file, rootItem); 
    } 

    treeView.setRoot(rootItem); 
} 

BTW: Ihr Problem durch die Erstellung der Baumstruktur verursacht wird, und ignorieren Sie es für jeden Knoten außer dem Stamm (für Nicht-root-Elemente der einzige Knoten, der rootItem hinzugefügt wurde; für andere Elemente als die Wurzel, die Sie hinzufügen, ist die "flache" dirCheckBoxTreeItem an die Eltern statt).