×

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 Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 minutes read.

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 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 Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

4 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape...

3 minutes read.

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 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.

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors...

4 minutes read.

JavaFX Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX Translate Transition

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

3 minutes read.

JavaFX Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 minutes read.

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.