0

I have built a small application with JavaFX and using FileChooser that allows the user to load Video files and Sound files and play them like any other media player.

The following method does this perfectly, and adds functionality to it:

abrir.setOnAction(e -> {
        view.getMediaPlayer().stop();
        String path1 = fc.showOpenDialog(stage).getAbsolutePath();
        final Media media1 = new Media(new File(path1).toURI().toString());
        final MediaPlayer player1 = new MediaPlayer(media1);
        player1.play();
        view.setMediaPlayer(player1);
        player1.currentTimeProperty().addListener((ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) -> {
            slider.setValue(newValue.toSeconds());
        });
        slider.setOnMouseClicked((MouseEvent mouseEvent) -> {
            player1.seek(Duration.seconds(slider.getValue()));
        });

    });

When a button is clicked, the user can succesfully load a media file into the media player and the file will be displayed without bugs on the HBox. The HBox was already added on top of the Scene. This works.

But with the method that allows the user to load image files(.jpg, .jpeg etc) it doesn't work. It doesn't produce an error and doesn't make any difference at all. Nothing is shown on the HBox.

abrir.setOnAction(e -> {
        File file = fc.showOpenDialog(null);
        BufferedImage bufferedImage;
        try {
            bufferedImage = ImageIO.read(file);
            Image imagem = SwingFXUtils.toFXImage(bufferedImage, null);
            visao.setImage(imagem);
        } catch (IOException ex) {
            Logger.getLogger(ChildMethod.class.getName()).log(Level.SEVERE, null, ex);
        }
    });

I have tried using only one method for this, but it doesn't work. The FileChooser window displays the image files, but when the user opens them, nothing happens. The HBox is empty.

Coder
  • 9
  • 3
  • You do know that [the `Image` constructor](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html#Image-java.lang.String-) takes a URL to??? No need load the image data twice into memory... – fabian Feb 28 '17 at 18:04
  • I was messing with my code to see if anything would work. – Coder Feb 28 '17 at 18:08
  • I already solved my problem. Using the exact same code, but put in a different class. When I need to open a video file, I call the class with the video methods. When I need to open an image file, I call the class with the image methods. Please, mark as solved. – Coder Feb 28 '17 at 19:37

0 Answers0