JavaFX | FileChooser Class
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 intial 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:
- 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 Program to create fileChooser// and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.geometry.*;importjavafx.scene.paint.*;importjavafx.scene.canvas.*;importjavafx.scene.text.*;importjavafx.scene.Group;importjavafx.scene.shape.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjava.io.*;importjavafx.stage.FileChooser;publicclassFileChooser_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("FileChooser");// create a File chooserFileChooser fil_chooser =newFileChooser();// create a LabelLabel label =newLabel("no files selected");// create a ButtonButton button =newButton("Show open dialog");// create an Event HandlerEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// get the file selectedFile file = fil_chooser.showOpenDialog(stage);if(file !=null) {label.setText(file.getAbsolutePath()+" selected");}}};button.setOnAction(event);// create a ButtonButton button1 =newButton("Show save dialog");// create an Event HandlerEventHandler<ActionEvent> event1 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// get the file selectedFile file = fil_chooser.showSaveDialog(stage);if(file !=null) {label.setText(file.getAbsolutePath()+" selected");}}};button1.setOnAction(event1);// create a VBoxVBox vbox =newVBox(30, label, button, button1);// set Alignmentvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,800,500);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:



- 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 Program to create FileChooser// & set title, initial File// and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.geometry.*;importjavafx.scene.paint.*;importjavafx.scene.canvas.*;importjavafx.scene.text.*;importjavafx.scene.Group;importjavafx.scene.shape.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;importjava.io.*;importjavafx.stage.FileChooser;publicclassFileChooser_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("FileChooser");// create a File chooserFileChooser fil_chooser =newFileChooser();// set titlefil_chooser.setTitle("Select File");// set initial Filefil_chooser.setInitialDirectory(newFile("e:\\"));// create a LabelLabel label =newLabel("no files selected");// create a ButtonButton button =newButton("Show open dialog");// create an Event HandlerEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// get the file selectedFile file = fil_chooser.showOpenDialog(stage);if(file !=null) {label.setText(file.getAbsolutePath()+" selected");}}};button.setOnAction(event);// create a ButtonButton button1 =newButton("Show save dialog");// create an Event HandlerEventHandler<ActionEvent> event1 =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// get the file selectedFile file = fil_chooser.showSaveDialog(stage);if(file !=null) {label.setText(file.getAbsolutePath()+" selected");}}};button1.setOnAction(event1);// create a VBoxVBox vbox =newVBox(30, label, button, button1);// set Alignmentvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,800,500);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:



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
Recommended Posts:
- JavaFX | Pos Class
- JavaFX | Tab Class
- JavaFX | HTMLEditor Class
- JavaFX | SepiaTone Class
- JavaFX | ColorAdjust Class
- JavaFX | Slider Class
- JavaFX | Hyperlink Class
- JavaFX | TextAlignment Class
- JavaFX | Font Class
- JavaFX | FontPosture Class
- JavaFX | CycleMethod Class
- JavaFX | VLineTo Class
- JavaFX | ImagePattern Class
- JavaFX | LinearGradient Class
- JavaFX | Stop Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



