JavaFX – FileChooser


JavaFX – FileChooser



”;


A file chooser is a graphical user interface elements that allow users to browse through file system. Generally, it is used to open and save either single or multiple files. In the below figure, we can see a file chooser in google drive application −


Filechooser

FileChooser in JavaFX

In JavaFX, the file chooser is represented by a class named FileChooser which belongs to a package named javafx.scene.control. We can create a file chooser component within our JavaFX application by instantiating this class. This class has only one constructor, i.e. its default constructor. However, it provides multiple properties, which are listed below −

  • initialDirectory − This property specifies the initial directory of the file chooser. You can set value to it using the setInitialDirectory() method.

  • selectedExtensionFilter − This property specifies the extension filter displayed in the dialog. You can set value to it using the setSelectedExtensionFilter() method.

  • Title − The property specifies the title of the dialog. can set value to it using the setTitle() method.

How to create a FileChooser in JavaFX?

Follow the steps given below to create a file chooser in JavaFX.

Step 1: Create a node to associate the FileChooser

A file chooser must be associated with another node, like menu or button, so that when the node is clicked, a dialog window opens for file selection. For this purpose, we have used a Menu as shown in the below code −


//Creating a menu
Menu fileMenu = new Menu("File");
//Creating menu Items
MenuItem item = new MenuItem("Open Image");

Step 2: Instantiate the FileChooser class

To create a file chooser, instantiate the FileChooser class. Then, set the desired file extension with the help of getExtensionFilters() method as shown in the following code block −


//Creating a File chooser
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Image");
fileChooser.getExtensionFilters().addAll(new ExtensionFilter("All Files", "*.*"));

Step 3: Add action handler to the Menu

It is important to set an action to the menu as it will trigger the opening of the file chooser dialog.


//Adding action on the menu item
item.setOnAction(new EventHandler<ActionEvent>() {
   public void handle(ActionEvent event) {
      //Opening a dialog box
      fileChooser.showOpenDialog(stage);
   }});

Step 4: Launch the Application

Once the file chooser is created and its properties are set, create a MenuBar. Next, pass the Menubar object to the constructor of Group class. Then, set the Scene and Stage. Lastly, launch the application with the help of the launch() method.

Example

Following is the program that will create a FileChooser using JavaFX. Save this code in a file with the name JavafxFilechooser.java.


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.FileChooser.ExtensionFilter;
public class JavafxFilechooser extends Application {
   public void start(Stage stage) {
      //Creating a menu
      Menu fileMenu = new Menu("File");
      //Creating menu Items
      MenuItem item = new MenuItem("Open Image");
      fileMenu.getItems().addAll(item);
      //Creating a File chooser
      FileChooser fileChooser = new FileChooser();
      fileChooser.setTitle("Open Image");
      fileChooser.getExtensionFilters().addAll(new ExtensionFilter("All Files", "*.*"));
      //Adding action on the menu item
      item.setOnAction(new EventHandler<ActionEvent>() {
         public void handle(ActionEvent event) {
            //Opening a dialog box
            fileChooser.showOpenDialog(stage);
      }});
      //Creating a menu bar and adding menu to it.
      MenuBar menuBar = new MenuBar(fileMenu);
      menuBar.setTranslateX(3);
      menuBar.setTranslateY(3);
      //Setting the stage
      Group root = new Group(menuBar);
      Scene scene = new Scene(root, 400, 300, Color.BEIGE);
      stage.setTitle("File Chooser Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Compile and execute the saved Java file from the command prompt using the following commands.


javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxFilechooser.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxFilechooser

Output

On executing, the above program displays a Button. When we click that button, it will show a pop-up window allowing the user to choose a file.


Filechooser Output

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *