微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在阶段之间传递价值JavaFxEclipse

如何解决在阶段之间传递价值JavaFxEclipse

我有两个.fxml和两个Controller。

首先,显示来宾信息(蓝色),每当单击“添加”按钮时,它将弹出另一个阶段(灰色/白色)以搜索具有ICnumber的来宾。

enter image description here

我的计划当我完成搜索访客(灰色)时,我将单击“添加访客”按钮,阶段(灰色)将关闭,所有值都将传递到访客信息(蓝色)。

    @FXML //Guest information (blue stage)
void addGuest(ActionEvent event) throws IOException {
    iclb.setText("ok"); //ignore this
    
    Parent root = FXMLLoader.load(getClass().getResource("Guest.fxml"));

    Scene scene = new Scene(root);

    Stage stage = new Stage();
    stage.setScene(scene);
    stage.setTitle("Hotel System - Guest information");
    stage.setResizable(false);
    stage.show();
}

public void addGuestInfo(String icno) {
    iclb.setText(icno); // this is the label for IC Number
}

-

    @FXML //Search Guest (grey stage)
void addGuest(ActionEvent event) throws IOException {
    
    FXMLLoader loader = new FXMLLoader(getClass().getResource("MenuDraft.fxml"));

    Parent root = loader.load();
    MenuController menu = loader.getController();
    menu.addGuestInfo(ictf.getText());

    Stage stage = (Stage) addbt.getScene().getwindow();
    stage.close();
}

到目前为止,我设法通过单击“添加访客”按钮来关闭访客,并能够关闭灰色阶段,但是这些值并未传递给访客信息(蓝色阶段)。

使用Javafx是我的新手吗?有人可以帮我吗?

解决方法

在第二个(“灰色”)控制器中,您可以执行以下操作:

public class AddGuestController {

    // I assume you have a Guest class to encapsulate the data in the form
    private Guest guest = null ;
    public Guest getGuest() {
        return guest ;
    }

    @FXML
    private void addGuest(ActionEvent event) {
        guest = new Guest( /* data from form controls... */ );
        addbt.getScene().getWindow().hide();
    }
}

然后回到第一个(“蓝色”)控制器,您可以使用stage.showAndWait()阻止执行直到窗口关闭,并检查是否创建了访客:

@FXML //Guest Information (blue stage)
private void addGuest(ActionEvent event) throws IOException {
    
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Guest.fxml"));
    Parent root = loader.load();

    Scene scene = new Scene(root);

    Stage stage = new Stage();
    stage.setScene(scene);
    stage.setTitle("Hotel System - Guest Information");
    stage.setResizable(false);
    stage.showAndWait();

    AddGuestController controller = loader.getController();
    Guest guest = controller.getGuest();
    if (guest != null) {
        // update view from guest:

        iclb.setText(guest.getIcNumber();
        // etc. 
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。