JavaFX | FileChooser Class

04 Oct, 2021
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: 
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 stageimport 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 applicationpublic 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 Methodpublic 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 stageimport 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 applicationpublic 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 Methodpublic 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 
simranarora5sos
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java
Stack Class in Java