Tech and Media Labs
This site uses cookies to improve the user experience.

JavaFX DirectoryChooser

Jakob Jenkov
Last update: 2019-01-24

A JavaFX DirectoryChooser is a dialog that enables the user to select a directory via a file explorer from the user's local computer. The JavaFX DirectoryChooser is implemented in the class javafx.stage.DirectoryChooser. In this JavaFX DirectoryChooser tutorial I will show you how to use the DirectoryChooser dialog.

Here is an example screenshot of how a JavaFX DirectoryChooser looks:

A JavaFX DirectoryChooser screenshot

Creating a DirectoryChooser

In order to use the DirectoryChooser you must first create a DirectoryChooser instance. Here is an example of creating a JavaFX DirectoryChooser:

DirectoryChooser directoryChooser = new DirectoryChooser();

Showing the DirectoryChooser Dialog

In order to make the DirectoryChooser visible you must call its showDialog() method. Here is an example of showing a JavaFX DirectoryChooser:

File selectedDirectory = directoryChooser.showDialog(primaryStage);

The File returned by the showDialog() method represents the directory the user selected in the DirectoryChooser.

The stage parameter is the JavaFX Stage that should "own" the DirectoryChooser dialog. By "owning" is meant what Stage from which the DirectoryChooser dialog is shown. This will typically be the Stage in which the button sits that initiates the showing of the DirectoryChooser.

Showing a DirectoryChooser is typically done as a result of a click on a button or menu item. Here is a full JavaFX example that shows a button that opens a DirectoryChooser when it is clicked:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;


public class DirectoryChooserExample extends Application {
    public static void main(String[] args) {

    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setInitialDirectory(new File("src"));

        Button button = new Button("Select Directory");
        button.setOnAction(e -> {
            File selectedDirectory = directoryChooser.showDialog(primaryStage);


        VBox vBox = new VBox(button);
        //HBox hBox = new HBox(button1, button2);
        Scene scene = new Scene(vBox, 960, 600);



Setting Initial Directory

You can set the initial directory of the JavaFX DirectoryChooser, meaning the root directory the DirectoryChooser will be located at when opened. This is also shown in the example above. You set the initial directory via the method setInitialDirectory(). Here is an example of setting the initial directory of a JavaFX DirectoryChooser:

directoryChooser.setInitialDirectory(new File("data/json/invoices"));

This example will set the initial directory of the given DirectoryChooser to data/json/invoices .

Jakob Jenkov

Copyright  Jenkov Aps
Close TOC