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.