将数据传递给控制器​​JAVAFX [英] Passing data to the controller JAVAFX

查看:101
本文介绍了将数据传递给控制器​​JAVAFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图片我正试图从一个传递数据(变量)阶段到另一个阶段,但当我尝试在第二阶段访问它们时,它们是空的。主窗口的代码。转到window1

my imageI'm trying to pass the data (variables) from one stage to another stage, but when I try to access them in the second stage they are null. Code of the mainWindow. Go to window1

 public class PrincipalController {
 private Stage primaryStage;
 public void initStage( Stage stage){ primaryStage = stage;}



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

                     // Access to window driver 1


 Window1Controller window1 = miCargador.
                  <window1Controlador>getController();
      windnow1.initStage(primaryStage);
      Scene scene = new Scene(root);
      primaryStage.setScene(scene);
      primaryStage.show();
     } catch (IOException e) {e.printStackTrace();}
    }
}

// window1 class:

// window1 class:

 public class Window1Controlador {
    private Stage primaryStage;
    private Scene escenaAnterior;
    private String tituloAnterior;
    public void initStage(Stage stage){
         primaryStage = stage;
         escenaAnterior = stage.getScene();
         tituloAnterior = stage.getTitle();
         primaryStage.setTitle("Window 1");
     }

如果我想访问之前收到的标题,则为空

If I want to access previous title that I received comes null

/**
     * Initializes the controller class.
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
       System.out.println(""+ this.tituloAnterior);}

初始化显示为null时应显示我Window1
}

The initialize shows me null when it should show me "Window1" }

推荐答案

initialize()方法在加载FXML文件的过程中被调用 - 换句话说,当你调用 miCargador.load()时调用它。

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().

显然之前发生了你调用 window1.initStage(...),所以当 initialize()被调用, tituloAnterior 仍为空。

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

简单的解决方案只是不要在 initialize()方法中访问 tituloAnterior ,而是要做任何你需要做的事情。 initStage()方法。例如,

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);
 }






如果您愿意,可以可以在Java代码中为FXML加载器设置控制器:


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();}
    }
}

然后从FXML文件中删除 fx:controller 属性。这样在 load()方法之前调用 initStage()方法,并且 tituloAnterior c>初始化()时,不会为空。

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.

这篇关于将数据传递给控制器​​JAVAFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆