I'm making a simple graphical desktop application for image management and, I'm using JavaFX, in addition to its sceneBuilder. The idea is, at pushing a button a FileChooser appears, to choose the image and to show a new window with all the background image on it. The problem is that in doing it. I get an error that I can not identify.
Edit: I discovered that if I open the image in the same window there is no error.
My code:
public void OpenWindow(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Window2.fxml"));
Scene secondScene = new Scene(root,800,800);
Stage newWindow = new Stage();
newWindow.setTitle("Imagen");
newWindow.setScene(secondScene);
newWindow.setX(100);
newWindow.setY(100);
newWindow.show();
/////// Open Window //////
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter(" JPG", "*.JPG");
fileChooser.getExtensionFilters().addAll(extFilterJPG);
File file = fileChooser.showOpenDialog(null);
try {
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
myImageView.setImage(image);
}catch(IOException ex) {
System.out.println(ex.getMessage());
}
}
Edit: (SOLUTION WAS CREATE A NEW FMXL LOADER) `
FXMLLoader loader = new FXMLLoader(getClass().getResource("SecondController.fxml"));
Parent root = (Parent) loader.load();
SecondController secController = loader.getController();
secController.nuevaImagen(imagen);
secController.mostrarInfo(imagen); // Hacer que el controlador de la imagen muestre la info
secController.addMainController(this);
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.setTitle(datosImagenActiva.titulo);
stage.show();`