问题描述
我有两个.fxml和两个Controller。
首先,显示来宾信息(蓝色),每当单击“添加”按钮时,它将弹出另一个阶段(灰色/白色)以搜索具有ICnumber的来宾。
我的计划当我完成搜索访客(灰色)时,我将单击“添加访客”按钮,阶段(灰色)将关闭,所有值都将传递到访客信息(蓝色)。
@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.
}
}