×

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a place wherever you want.

In order to use FileChooser in JavaFX, javafx.stage.FileChooser class is used.

The main two methods present in the FileChooser class are:

  1. showOpenDialog(): This method is used to open the FileChooser, so that the user can choose the file which is to be opened.
  2. showSaveDialog(): This method is used to open the save option in FileChooser, so that the user can save the file.

JavaFX Open FileChooser:

Example:

import javafx.application.Application;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
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.scene.layout.StackPane;


  
public class FileUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Label newlabel = new Label(" Choose File: ");  
           TextField newtf= new TextField();  
           Button newbtn = new Button(" Browse ");  
           newbtn.setOnAction(e->  
           {  
               FileChooser newfile = new FileChooser();  
               newfile.setTitle(" Open File you want ");  
               newfile.showOpenDialog(primaryStage);  
           });  
           HBox box = new HBox();  
             
           box.setSpacing(20);  
           box.getChildren().addAll(newlabel,newtf,newbtn);  
           Scene scene = new Scene(box,400,200);  
           primaryStage.setScene(scene);  
           primaryStage.setTitle("JavaFX FileChooser Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the FileChooser in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.FileChooser, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.control.Button, javafx.scene.control.Label, javafx.scene.control.TextField, javafx.scene.layout.HBox, and javafx.scene.layout.StackPane.

Then we have created one class named FileUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold the Label, Text field, and button, an HBox object is created which is then passed to the Scene class object.

The label is created to choose the file with one text field along with it. Also, one button is created with an action set on it to open FileChooser.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX FileChooser Example”. Also, it displays one label to choose file along with text field and Browse button. On clicking on the Browse button it will allow us to choose the file from any location in the system. And it can be opened by clicking on the Open button.

JavaFX FileChooser

JavaFX Saving File:

Example:

import java.io.File;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;


  
public class FileUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      
           Button newbtn = new Button("SAVE");  
           newbtn.setOnAction(e->  
           {  
               FileChooser newfile = new FileChooser();  
               newfile.setTitle(" Save File ");  
               File file1 = newfile.showSaveDialog(primaryStage);  
               System.out.println(file1);  
           });  
           HBox box = new HBox();  
             
           box.setSpacing(20);  
           box.getChildren().addAll(newbtn);  
           Scene scene = new Scene(box,400,200);  
           primaryStage.setScene(scene);  
           primaryStage.setTitle("JavaFX Saving File Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the FileChooser to save file in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.FileChooser, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.control.Button, java.io.File, javafx.scene.layout.HBox, and javafx.scene.layout.StackPane.

Then we have created one class named FileUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold the Save button, an HBox object is created which is then passed to the Scene class object.

The save button is created with an action set on it to save the file and print the location of the file in the console.

The stage is prepared, the title is set and the show() method is called to display output. The launch(args) method is called. In output Frame like container is displayed with the title, "JavaFX Saving File Example”. Also, it displays one save button by clicking on which it allows us to save the image. After clicking on the save button it will show the path of the file in the system on the console.

JavaFX FileChooser JavaFX FileChooser

Related Topics

JavaFX Selectors

In the JavaFX application, in order to apply various css effect, we use the various css classes available in the JavaFX. This is generally used to enhance the appearance property...

4 minutes read.

JavaFX Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Rotate Transition

In the JavaFX application, in order to generate the rotate transition, we have to apply rotation transition on any particular shape. It has the ability to rotate the shape along...

4 minutes read.

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.

JavaFX Circle

In the JavaFX application, in order to draw a circle, the Circle class is used. The Circle allows us to join any points in the space. All methods needed for...

4 minutes read.

JavaFX Area Chart

In the JavaFX application, in order to create Area Chart, the AreaChart class is used. The Area Chart allows us to plot data along the XY plane. All methods needed...

3 minutes read.

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

4 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

4 minutes read.

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 minutes read.

JavaFX Light Point

In the JavaFX application, in order to apply various LightPoint effect from a single source, we use this class. It has the ability to apply light from a single source...

4 minutes read.

JavaFX Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Motion Blur

In the JavaFX application, in order to apply various Motion blur effect, we use Motion Blur class. It has the ability to apply the Motion blur effect on the given...

4 minutes read.

JavaFX Drop Shadow

In the JavaFX application, in order to apply various Drop Shadow effect, we use this class. It has the ability to create the drop shadow of the given object. This...

4 minutes read.

JavaFX Light Spot

In the JavaFX application, in order to apply various Light Spot effect from a single source, we use this class. It has the ability to apply light from all the...

4 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

4 minutes read.