JavaFX File Chooser

0

JavaFX File Chooser tutorial:

JavaFX File Chooser allow the user to choose a file or navigating the file system to open or save.

It is used for selecting single or multiple file and for file save dialog.

Constructors :

FileChooser().

Extension Filter :

  • Extension filter only allow user to opening file of given extension.
  • ExtensionFilter extensionFilter=new ExtensionFilter("JPG", "*.jpg");
    fileChooser.getExtensionFilters().add(extensionFilter);
  • It create an extensionFilter for “.jpg” and add it to the fileChooser.

File Opening :

  • To open a file using FileChooser first we create a new FileChooser AS
    FileChooser fileChooser = new FileChooser();
  • And the set a title to the fileChooser AS
    fileChooser.setTitle("Choose a File");
  • And then show the popup window for opening file.
    fileChooser.showOpenDialog(stage);

Example of file chooser:


package javafxtuts;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

public class Javafxtuts extends Application {

  public static void main(String[] args) {
     Application.launch(args);
  }

  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      Label label =new Label("A File Chooser");
      FileChooser fileChooser = new FileChooser();
      fileChooser.setTitle("Choose a File");
      //to create an extensionFilter for ".jpg" and add it to the fileChooser.
      ExtensionFilter extensionFilter=new ExtensionFilter("JPG", "*.jpg");
      fileChooser.getExtensionFilters().add(extensionFilter);
      //for showing the popup window to open file
      File file=fileChooser.showOpenDialog(stage);
      label.setText("You have opend a file "+file);
      BorderPane root = new BorderPane();
      root.setCenter(label);
        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.show();
  }
}

Output :

Explanation of file chooser example:

  • Creating a fileChooser and set title of fileChooser as
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Choose a File");
  • Creating a extensionFilter for “.jpg”
    fileChooser.ExtensionFilter extensionFilter=new ExtensionFilter("JPG", "*.jpg");
  • Adding a extensionFilters to the fileChooser using getExtensionFilters() which return a list of extensionFilters and using add() add themfileChooser.getExtensionFilters().add(extensionFilter);
  • fileChooser.showOpenDialog(stage);It show a new file open dialog and return the selected file.
  • So we store the returned file in the file variable.
  • And then print the file using label in the application aaslabel.setText("You have opend a file "+file);

Saving File :

As we know FileChooser also used to save a file.
All the things are almost same as Opening File but difference is

fileChooser.showSaveDialog(stage);
The showSaveDialog() method of the FileChooser class open a save dialog window.

Example of saving file:


package javafxtuts;
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

public class Javafxtuts extends Application {

  public static void main(String[] args) {
     Application.launch(args);
  }

  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      Label label =new Label("A File Chooser");
      FileChooser fileChooser = new FileChooser();
      fileChooser.setTitle("Save File");
      //to create an extensionFilter for ".jpg" and add it to the fileChooser.
      ExtensionFilter extensionFilter=new ExtensionFilter("JPG", "*.jpg");
      fileChooser.getExtensionFilters().add(extensionFilter);
      //for showing the popup window to Save file
      File file = fileChooser.showSaveDialog(stage);
      label.setText("You have saved a file "+file);
      BorderPane root = new BorderPane();
      root.setCenter(label);
        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.show();
  }
}

Output :

Explanation :
  • All are same as in the open file explain But
  • fileChooser.showSaveDialog(stage);It open a save dialog window.
Share.

Leave A Reply