JavaFX Tutorial - JavaFX FileChooser

The FileChooser allows users to navigate the file system and choose a file or a folder.

The FileChooser class is located in the javafx.stage package.

Opening Files

A file chooser can be used to as an open file dialog for selecting either a single file or multiple files, or as a file save dialog.

The following code creates a FileChooser object and set its title and then display to the users.

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

We can configure the file chooser dialog window by setting the initialDirectory and title properties.

Extension Filters

We can set the extension filter to determine which files to open in a file chooser.

fileChooser.setTitle("View Pictures");
fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))
    new FileChooser.ExtensionFilter("All Images", "*.*"),
    new FileChooser.ExtensionFilter("JPG", "*.jpg"),
    new FileChooser.ExtensionFilter("GIF", "*.gif"),
    new FileChooser.ExtensionFilter("BMP", "*.bmp"),
    new FileChooser.ExtensionFilter("PNG", "*.png")

Saving Files

The FileChooser API allows a user to specify a file name and its folder for a file to be saved by the application.

The showSaveDialog method opens a save dialog window.

FileChooser fileChooser1 = new FileChooser();
fileChooser1.setTitle("Save Image");
File file = fileChooser1.showSaveDialog(stage);


/*from  w  w  w  .  j  ava2s .com*/
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextAreaBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
    public static void main(String[] args) {
    public void start(final Stage primaryStage) {
        Group root = new Group();
        Button buttonLoad = new Button("Load");
        buttonLoad.setOnAction(new EventHandler<ActionEvent>(){
            public void handle(ActionEvent arg0) {
                FileChooser fileChooser = new FileChooser();
                FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
                File file = fileChooser.showOpenDialog(primaryStage);
        VBox vBox = VBoxBuilder.create()
        primaryStage.setScene(new Scene(root, 500, 400));;


The code above generates the following result.