Issue
I have two .fxml and two Controller.
At first, the Guest Information (blue) is shown and whenever the "Add" button is clicked, then it will pop up another stage (grey/white) to search for a Guest with ICnumber.
My plan when I done searching for a Guest(grey), I will click the "Add Guest" button, the stage(grey) will close and all the value will be passed to the Guest Information (blue).
@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();
}
So far I manage to search a Guest and able to close the grey stage by clicing the "Add Guest" button, but those value didn't pass to the Guest Information (blue stage).
Im very new in using Javafx... anyone can give me a hand on this?
Solution
In your second ("grey") controller you can do this:
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();
}
}
Then back in the first ("blue") controller, you can use stage.showAndWait()
to block execution until the window closes, and check if a guest was created:
@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.
}
}
Answered By - James_D