Issue
I have 2 .fxml files (stage1.fxml and stage2.fxml) and its controller class.
I have a button in stage1.fxml which opens stage2.fxml when clicked, it has this code on it:
public void btnStage2(ActionEvent event) {
try {
Parent stage2 = FXMLLoader.load(getClass().getResource("/application/stage2.fxml"));
Stage stage = new Stage();
stage.setScene(new Scene(stage2));
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
this opens stage2
just fine, however the stage1
is still open and I can just tab into it and still use the buttons in that stage, and I don't want that.
How can I .setVisible(false) the stage1
when I press the btnStage2
just before opening the stage2.fxml?
I found other posts regarding "hiding" the stage instead of closing it when I pressed the close button, but that doesn't match my need. Thanks for the help!
Solution
after hours of trial and error, I think I finally understood how stage.close() works.
the solution to my problem is to just slap these 2 lines of code in the method for the button:
Stage stage1= (Stage) btnStage2.getScene().getWindow();
stage1.close();
Previously, I thought that I cannot call multiple Stage
in a single function. Turns out I was wrong.
Answered By - itskenleton