JavaFX FileChooser and DirectoryChooser

A FileChooser is available as part of the JavaFX API.

Example usage from javadoc:

FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");

fileChooser.getExtensionFilters().addAll(
        new ExtensionFilter("Text Files", "*.txt"),
        new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"),
        new ExtensionFilter("Audio Files", "*.wav", "*.mp3", "*.aac"),
        new ExtensionFilter("All Files", "*.*"));

File selectedFile = fileChooser.showOpenDialog(mainStage);

if (selectedFile != null) {
    mainStage.display(selectedFile);
}

A DirectoryChooser was added to JavaFX as part of the 2.1 release.

Usage is:

DirectoryChooser chooser = new DirectoryChooser();
chooser.setTitle("JavaFX Projects");

File defaultDirectory = new File("c:/dev/javafx");
chooser.setInitialDirectory(defaultDirectory);

File selectedDirectory = chooser.showDialog(primaryStage);

The issue tracker mentions a work-around for the 2.0GA release: “accessing the private Oracle API Glass method CommonDialogs.showFolderChooser“.


Both the DirectoryChooser and FileChooser will internally be implemented using the native file and directory choosing user interface dialogs provided by the Operating System (they are not actually implemented as JavaFX stages with a SceneGraphs).

This means that the look and feel of these elements will differ depending on the operating system platform and native window management toolkit that JavaFX is running on top of.

Leave a Comment