Introduction to JavaFX FileChooser
In JavaFX, FileChooser is a class that is used to browse the files from the system. Several operations include opening a single file, opening multiple files and saving files in the system. JavaFX file chooser is instantiated from the class javafx.stage.FileChooser. The syntax, states, constructors, and example of JavaFX FileChooser will be discussed in the following sections.
Syntax:
JavaFX file chooser can be created using the below syntax:
FileChooser fl = new FileChooser();
Make sure the class javafx.stage.FileChooser is imported before writing the JavaFX program.
JavaFX FileChooser Constructors
There is only one constructor for JavaFX FileChooser. That is,
FileChooser(): A FileChooser dialog will be created
// create a FileChooser
FileChooser fl = new FileChooser();
Methods of JavaFX FileChooser
Following are some of the commonly used methods in JavaFX FileChooser:
- getTitle(): File chooser title will be returned.
- setTitle(String s): File chooser title will be set.
- getInitialDirectory(): File chooser’s initial directory will be returned.
- setInitialDirector (file f ): File chooser’s initial directory will be set.
- setInitialFileName(String s): File chooser’s initial file name will be set.
- getInitialFileName(): File chooser’s initial file name will be returned.
- showOpenDialog(Window wn ): A dialog appears with a new open file selection.
- showSaveDialog(Window wn): A dialog appears with a new save file selection.
How to Create a FileChooser?
There are several steps to create a FileChooser.
Step 1: Create a FileChooser. In order to create a FileChooser, the following syntax can be used:
FileChooser fc = new FileChooser();
Step 2: Create the vertical Box. In this step, create a vertical box as follows.
VBox vb = new VBox();
Step 3: Add FileChooser created to the Scene Graph. After the creation of a vertical box, add the FileChooser to the scene graph using the below steps.
//create a scene
Scene sce = new Scene(vb, 300, 200);
//set the scene
s.setScene(sce);
//displays the result
s.show();
Program to Implement JavaFX FileChooser
Now, let us have a look into the implementation of the JavaFX file chooser:
Example #1
Java program to demonstrate FileChooser that browse the file location.
Code:
//Java program to demonstrate file chooser that browse the file location
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
//sample class that extends Application classs
public class JavaFXfilechooserexample extends Application{
//application starts at this point
@Override
public void start(Stage s) throws Exception {
//create a file chooser
FileChooser f = new FileChooser();
//set the title
f.setTitle("Opening the location..");
//open the dialog box
f.showOpenDialog(s);
HBox r = new HBox();
r.setSpacing(20);
//create scene
Scene sc = new Scene(r,350,100);
//set the scene
s.setScene(sc);
//set title
s.setTitle("Sample file chooser");
//display the result
s.show();
}
//main method
public static void main(String[] args) {
launch(args);
}
}
Output: A dialog box appears in which the user can select the location to browse any file they need. Also, it can be seen that a file title is displayed in the top left of the dialog box.
Example #2
The program that chooses a file and save it with the help of a button and an event handler.
Code:
//Program that chooses a file and save it with the help of a button and an event handler.
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.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import java.io.*;
import javafx.stage.FileChooser;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;
import javafx.scene.text.*;
import javafx.scene.Group;
import javafx.scene.shape.*;
public class JavaFXfilechooserexample extends Application {
// application starts at this point
public void start(Stage s)
{
// A title is set for the stage
s.setTitle("FileChooser Example");
// File chooser creation
FileChooser fc = new FileChooser();
//Title creation for dialog box
fc.setTitle("Select the File you want . . .");
// initial File which is in E directory
fc.setInitialDirectory(new File("e:\\"));
// Label creation
Label l = new Label("You didn't select any files");
// Button 1 creation
Button b1 = new Button("Show dialog");
// Event Handler creation for button 1
EventHandler<ActionEvent> ev = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
// Retrieve the file selected by the user
File file = fc.showOpenDialog(s);
//condition that will be selected only if the user has choosen a file
if (file != null) {
//a text that contains the file that is selected
l.setText("You have selected the file " +file.getAbsolutePath());
}
}
};
//action performs when button 1 is clicked
b1.setOnAction(ev);
// Button 2 creation
Button b2 = new Button("Show save dialog");
// Event Handler creation for button 2
EventHandler<ActionEvent> ev2 = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
// Retrieve the file selected by the user
File file = fc.showSaveDialog(s);
//condition that will be selected only if the user has choosen a file
if (file != null) {
l.setText("You have selected the file " +file.getAbsolutePath()
);
}
}
};
//action performs when button is clicked
b2.setOnAction(ev2);
// VBox creation
VBox vb = new VBox(30, l, b1, b2);
// Alignment setting
vb.setAlignment(Pos.CENTER);
// scene creation
Scene sc = new Scene(vb, 800, 500);
// scene setting
s.setScene(sc);
//output display
s.show();
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output:
A dialog box as shown below will appear on executing the code.
On clicking the button Show dialog, the E folder will be opened with an option to select a file. (E folder is selected as we have mentioned it in the code).
Select the file based on your requirements. For demo purposes, I have chosen the file as shown below.
After selecting, there will be a change in the label of the dialog box.
It can be clearly seen that the label changes with the file name I have selected.
On clicking the Show Save dialog button, the location to save the file that is chosen has to be selected.
Then the save button can be clicked.
Conclusion
In JavaFX, File choosers are used to browsing and save single files and multiple files. Unlike others, it uses only a non-parameterized constructor. In this document, syntax, constructor, methods, and program to implement JavaFX File Chooser is clearly discussed.
Recommended Articles
This is a guide to JavaFX FileChooser. Here we discuss syntax, constructors, methods, and examples to implement with proper codes and outputs. You can also go through our other related articles to learn more –