Passing data to the controller JAVAFX

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 10:01:22

The initialize() method is called as part of the process of loading the FXML file - in other words it is called when you call miCargador.load().

Obviously this happens before you call window1.initStage(...), so when initialize() is invoked, tituloAnterior is still null.

The simple solution is just not to access tituloAnterior in the initialize() method, but to do whatever you need to do with it in the initStage() method. E.g.

public void initStage(Stage stage){
     primaryStage = stage;
     escenaAnterior = stage.getScene();
     tituloAnterior = stage.getTitle();
     primaryStage.setTitle("Window 1");
     someLabelFromFXML.setText(tituloAnterior);
 }

If you prefer, you could set the controller for the FXML loader in the Java code:

@FXML
private void goWindow1(ActionEvent event) {
    try {
         FXMLLoader miCargador = new
                 FXMLLoader(getClass().getResource("/vista/Window1.fxml"));

         Window1Controller window1 = new Window1Controller();
         window1.initStage(primaryStage);
         miCargador.setController(window1);

         Parent root = (Parent) miCargador.load();

                     // Access to window driver 1



      Scene scene = new Scene(root);
      primaryStage.setScene(scene);
      primaryStage.show();
     } catch (IOException e) {e.printStackTrace();}
    }
}

Then remove the fx:controller attribute from your FXML file. This way the initStage() method is called before the load() method, and tituloAnterior will not be null when initialize() is called.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!