How to make directories expandable in javafx TreeView


Question

I have a problem which I am not sure how to solve and could not find some clues online as well, but the problem should be trivial...

I have a TreeView<File> which I would like to fill with list of directories and files based on a given path. The problem is that the directories are added to the tree, but cannot be expanded, thus I cannot display the files inside.

Here is some of my humble controller code:

public class MainViewController implements Initializable {
    @FXML // fx:id="filesTree"
    private TreeView<File> filesTree;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        File currentDir = new File("src/xslt"); // current directory
        findFiles(currentDir);
    }

    public void findFiles(File dir) {
    TreeItem<File> root = new TreeItem<>(new File("Files:"));
    root.setExpanded(true);
    try {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("directory:" + file.getCanonicalPath());
                findFiles(file);
            } else {
                System.out.println("     file:" + file.getCanonicalPath());
                root.getChildren().add(new TreeItem<>(file));
            }
            root.getChildren().add(new TreeItem<>(file));
        }

        filesTree.setRoot(root);
        } catch (IOException e) {
           e.printStackTrace();
        }
    }

}

And my FXML view is really simple - just an AnchorPane with a TreeView. But I can share it as well if needed.

So the questions is - how do I make the directories expandable? I found setExpanded(true) method but that is different.

1
3
11/1/2014 2:34:11 PM

Accepted Answer

Basically, the recursive method has to create a new root every time you find a directory.

private void findFiles(File dir, TreeItem<File> parent) {
    TreeItem<File> root = new TreeItem<>(dir);
    ...
}

And this root has to be sent as parent for the next level.

    if (file.isDirectory()) {
        System.out.println("directory:" + file.getCanonicalPath());
        findFiles(file,root);
    }

Finally, only at the top most level we set this root as root of the treeView:

if(parent==null){
    filesTree.setRoot(root);
}

while in the inner levels:

else {
    parent.getChildren().add(root);
}

So after these few adjustements, this should be working:

@FXML private TreeView<File> filesTree;

@Override
public void initialize(URL url, ResourceBundle rb) {
    File currentDir = new File("src/xslt"); // current directory
    findFiles(currentDir,null);
}

private void findFiles(File dir, TreeItem<File> parent) {
    TreeItem<File> root = new TreeItem<>(dir);
    root.setExpanded(true);
    try {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("directory:" + file.getCanonicalPath());
                findFiles(file,root);
            } else {
                System.out.println("     file:" + file.getCanonicalPath());
                root.getChildren().add(new TreeItem<>(file));
            }

        }
        if(parent==null){
            filesTree.setRoot(root);
        } else {
            parent.getChildren().add(root);
        }
    } catch (IOException e) {
       e.printStackTrace();
    }
} 
8
11/1/2014 7:22:45 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon