JavaFX Program to Open Multiple Files at Once With Popup Dialog Using FileChooser Class GUI Desktop App

You are currently viewing JavaFX Program to Open Multiple Files at Once With Popup Dialog Using FileChooser Class GUI Desktop App

JavaFX Program to Open Multiple Files at Once With Popup Dialog Using FileChooser Class GUI Desktop App

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.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.FileChooser.ExtensionFilter;
public class FileChooserOpeningMultipleFiles extends Application {
   public void start(Stage stage) {
      ImageView imgView = new ImageView("UIControls/multiple_files.png");
      imgView.setFitWidth(20);
      imgView.setFitHeight(20);
      Menu file = new Menu("File");
      MenuItem item = new MenuItem("Open Multiple Files", imgView);
      file.getItems().addAll(item);
      //Creating a File chooser
      FileChooser fileChooser = new FileChooser();
      fileChooser.setTitle("Open Multiple Files");
      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.showOpenMultipleDialog(stage);
      }});
      //Creating a menu bar and adding menu to it.
      MenuBar menuBar = new MenuBar(file);
      Group root = new Group(menuBar);
      Scene scene = new Scene(root, 595, 355, Color.BEIGE);
      stage.setTitle("File Chooser Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply