Open In App

JavaFX | FileChooser Class

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

FileChooser class is a part of JavaFX. It is used to invoke file open dialogs for selecting a single file (showOpenDialog), file open dialogs for selecting multiple files (showOpenMultipleDialog) and file save dialogs (showSaveDialog). FileChooser class inherits Object class.

Constructor of the class are : 

  • FileChooser() : Creates a new FileChooser dialog.

Commonly Used Methods: 

Method Explanation
getInitialDirectory() Returns the initial directory of the file chooser.
getTitle() Returns the title of file chooser.
setInitialDirectory(File f) Sets the initial directory of the filechooser.
setTitle(String t) Sets the title of the file chooser.
showOpenDialog(Window w) Shows a new open file selection dialog.
setInitialFileName(String n) Sets the initial file name of file chooser.
showSaveDialog(Window w) Shows a new Save file selection dialog.
getInitialFileName() Returns the initial filename of the file chooser.

Below programs illustrate the use of FileChooser Class:

1. Java Program to create fileChooser and add it to the stage: In this program we will create a file chooser named file_chooser. Then create a Label named label and two Buttons named button and button1. We will create two EventHandler to handle the events when the button or button1 pressed. When the button is pressed, an open file chooser dialog appears and the selected file is shown as text in the label and when button1 is pressed, a save file chooser appears and the selected file is shown as text in the label. Add the label and the button to Vbox and add the VBox to the Scene and add the scene to the stage, and call the show() function to display the final results.

Java




// Java Program to create fileChooser
// and add it to the stage
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.geometry.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;
import javafx.scene.text.*;
import javafx.scene.Group;
import javafx.scene.shape.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import java.io.*;
import javafx.stage.FileChooser;
  
public class FileChooser_1 extends Application {
  
// launch the application
public void start(Stage stage)
{
 
    try {
 
        // set title for the stage
        stage.setTitle("FileChooser");
 
        // create a File chooser
        FileChooser fil_chooser = new FileChooser();
 
        // create a Label
        Label label = new Label("no files selected");
 
        // create a Button
        Button button = new Button("Show open dialog");
 
        // create an Event Handler
        EventHandler<ActionEvent> event =
        new EventHandler<ActionEvent>() {
 
            public void handle(ActionEvent e)
            {
 
                // get the file selected
                File file = fil_chooser.showOpenDialog(stage);
 
                if (file != null) {
                     
                    label.setText(file.getAbsolutePath()
                                        + "  selected");
                }
            }
        };
 
        button.setOnAction(event);
 
        // create a Button
        Button button1 = new Button("Show save dialog");
 
        // create an Event Handler
        EventHandler<ActionEvent> event1 =
         new EventHandler<ActionEvent>() {
 
            public void handle(ActionEvent e)
            {
 
                // get the file selected
                File file = fil_chooser.showSaveDialog(stage);
 
                if (file != null) {
                    label.setText(file.getAbsolutePath()
                                        + "  selected");
                }
            }
        };
 
        button1.setOnAction(event1);
 
        // create a VBox
        VBox vbox = new VBox(30, label, button, button1);
 
        // set Alignment
        vbox.setAlignment(Pos.CENTER);
 
        // create a scene
        Scene scene = new Scene(vbox, 800, 500);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    catch (Exception e) {
 
        System.out.println(e.getMessage());
    }
}
 
// Main Method
public static void main(String args[])
{
 
    // launch the application
    launch(args);
}
}


Output:

2. Java Program to create FileChooser, set title, initial File and add it to the stage: In this program we will create a file chooser named fil_chooser . Then create a Label named label and two Buttons named button and button1. Set the title and initial directory of file chooser using the setTitle() and setInitialDirectory() function. Now create two EventHandler to handle the events when the button or button1 pressed. When the button is pressed, an open file chooser dialog appears and the selected file is shown as text in the label and when button1 is pressed, a save file chooser appears and the selected file is shown as text in the label. Add the label and the button to Vbox and add the VBox to the Scene and add the scene to the stage, and call the show() function to display the final results.

Java




// Java Program to create FileChooser
// & set title, initial File
// and add it to the stage
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.geometry.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;
import javafx.scene.text.*;
import javafx.scene.Group;
import javafx.scene.shape.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import java.io.*;
import javafx.stage.FileChooser;
  
public class FileChooser_2 extends Application {
 
// launch the application
public void start(Stage stage)
{
 
    try {
 
        // set title for the stage
        stage.setTitle("FileChooser");
 
        // create a File chooser
        FileChooser fil_chooser = new FileChooser();
 
        // set title
        fil_chooser.setTitle("Select File");
 
        // set initial File
        fil_chooser.setInitialDirectory(new File("e:\\"));
 
        // create a Label
        Label label = new Label("no files selected");
 
        // create a Button
        Button button = new Button("Show open dialog");
 
        // create an Event Handler
        EventHandler<ActionEvent> event =
        new EventHandler<ActionEvent>() {
 
            public void handle(ActionEvent e)
            {
 
                // get the file selected
                File file = fil_chooser.showOpenDialog(stage);
 
                if (file != null) {
                    label.setText(file.getAbsolutePath()
                                        + "  selected");
                }
            }
        };
 
        button.setOnAction(event);
 
        // create a Button
        Button button1 = new Button("Show save dialog");
 
        // create an Event Handler
        EventHandler<ActionEvent> event1 =
         new EventHandler<ActionEvent>() {
 
            public void handle(ActionEvent e)
            {
 
                // get the file selected
                File file = fil_chooser.showSaveDialog(stage);
 
                if (file != null) {
                    label.setText(file.getAbsolutePath()
                                        + "  selected");
                }
            }
        };
 
        button1.setOnAction(event1);
 
        // create a VBox
        VBox vbox = new VBox(30, label, button, button1);
 
        // set Alignment
        vbox.setAlignment(Pos.CENTER);
 
        // create a scene
        Scene scene = new Scene(vbox, 800, 500);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    catch (Exception e) {
 
        System.out.println(e.getMessage());
    }
}
 
// Main Method
public static void main(String args[])
{
 
    // launch the application
    launch(args);
}
}


Output:

Note: The above programs might not run in an online IDE please use an offline compiler.

Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/FileChooser.html
 



Last Updated : 04 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads