JavaFX 8 Tutorial 46 - FileChooser





















The Code written in this tutorial is:



    private FileChooser fileChooser;

    private Button browse;

    private File file;

    private final Desktop desktop = Desktop.getDesktop();



fileChooser = new FileChooser();

        fileChooser.getExtensionFilters().addAll(

                new ExtensionFilter("Text Files", "*txt"),

                new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"),

                new ExtensionFilter("Audio Files", "*wav", "*.mp3", "*.aac"),

                new ExtensionFilter("All Files", "*.*")

        );

       

        browse = new Button("Browse");

        browse.setFont(Font.font("SanSerif", 15));

        browse.setOnAction(e ->{

            /*Single File Selection

            file = fileChooser.showOpenDialog(primaryStage);

            if(file != null){

                try {

                    desktop.open(file);

                } catch (IOException ex) {

                    Logger.getLogger(Tutorial14.class.getName()).log(Level.SEVERE, null, ex);

                }

            }*/

           

            //Multiple File Selection

            List<File> fileList = fileChooser.showOpenMultipleDialog(primaryStage);

            if(fileList != null){

                fileList.stream().forEach(selectedFiles ->{

                    try {

                        desktop.open(selectedFiles);

                    } catch (IOException ex) {

                        Logger.getLogger(Tutorial14.class.getName()).log(Level.SEVERE, null, ex);

                    }

                });

            }

        });


Comments

Popular posts from this blog

JavaFX 8 Tutorial 64 Spring Boot CRUD with FXML

JavaFX 8 Tutorial 62 - Jasper Report

JavaFX 8 Tutorial 51 - Export Database to Excel