JavaFx从第二个窗口获取返回

时间:2020-11-05 08:58:31

标签: user-interface javafx

我正在用JavaFx编程GUI,但我真的不知道如何连接所有内容。 我有一个主窗口,在主窗口中有一个按钮,可打开另一个窗口,让我可以选择客户。 现在,我已经很难将选择的客户的信息返回到主窗口,因为我没有返回方法可以打开它。

我以这种方式打开它: public void openSecondWindow(ActionEvent event){

     Parent root;
     try {
         root = FXMLLoader
                 
    .load(getClass().getResource("....secondWindow.fxml"));
        Stage stage = new Stage();
        stage.setTitle("Second Window");
        stage.setScene(new Scene(root));
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.show();

我自己没有激活第二个窗口,也没有像 SecondWindow sc =新的SecondWindow();所以我可以叫类似sc.getCustomer之类的东西。

解决起来可能很容易,但是我缺少合适的词汇来搜索它。

1 个答案:

答案 0 :(得分:0)

为了结束这个问题,我的解决方案如下所示:

SecondWindow sc = loader.getController();

然后我可以像 scOpenWithData(int id){....}

这样调用我修改过的方法

sc.scOpenWithData(123);

总体来说:

 Parent root;
 try {
     FXMLLoader loader = new FXMLLoader(
            getClass().getResource("....secondWindow.fxml"));
    root = loader.load();
    SecondWindow sc = loader.getController();
    Stage stage = new Stage();
    stage.setTitle("Second Window");
    stage.setScene(new Scene(root));
    stage.initModality(Modality.APPLICATION_MODAL);
    sc.scOpenWithData(123);
    stage.showAndWait();