1

Let's say I have a Stage A which contain two Buttons, oui Button opens a new Stage B, non closes A. what I'm trying to do is close A after clicking on oui Button and open B. I'm using showAndWait() to open B, then I try to execute A.close() which obviously fails being preceded by showAndWait(), when I tried to run A.close() before showAndWait() A closes, but then all Bcontrols including Buttons and Text Fields become inactive, is there any workaround ?

Here is the executed code when clicking on oui in order to open B Stage :

public class AController implements Initializable { @FXML private Button oui; @FXML private Button non; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { // TODO } @FXML private void ouiBtnClick(ActionEvent event) throws IOException { FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("B.fxml")); Stage stage = new Stage(); VBox mainPane = (VBox) loader.load(); Scene scene = new Scene(mainPane); stage.setScene(scene); stage.initStyle(StageStyle.DECORATED); stage.setResizable(false); stage.showAndWait(); nonBtnClick(); // method that close `A` } @FXML private void nonBtnClick() { Stage s = (Stage) non.getScene().getWindow(); s.close(); } } 
5
  • This should demonstrate what you are asking. Commented Aug 26, 2017 at 14:50
  • 1
    Also, what happens if you use show instead of showAndWait? Commented Aug 26, 2017 at 14:52
  • @SedrickJefferson I used show and it did the trick ! Thank you man, thank you ! Commented Aug 26, 2017 at 15:00
  • Write an answer and accept it. Commented Aug 26, 2017 at 15:02
  • 1
    I just did, I'll be able to accept it after two days. Commented Aug 26, 2017 at 15:07

2 Answers 2

1

Using show instead of showAndWait did the trick . Thanks to Sedrick Jefferson comment.

Sign up to request clarification or add additional context in comments.

Comments

0

According to my understanding ,you need to close current stage and open new stage.If you need to close stage A ,we can say current stage and open new stage ,we can say next stage ,you need get window of your event and close it ,i mean window of clickEvent

 @FXML private void ouiBtnClick(ActionEvent event) throws IOException { FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("B.fxml")); Stage stage = new Stage(); VBox mainPane = (VBox) loader.load(); Scene scene = new Scene(mainPane); stage.setScene(scene); stage.initStyle(StageStyle.DECORATED); stage.setResizable(false); stage.show(); nonBtnClick(event); } @FXML private void nonBtnClick(ActionEvent event) { ((Node) event.getSource()).getScene().getWindow().hide();//close currentstage } 

1 Comment

You just changed showAndWait() to show() which I stated in the answer, as for your last line, it does the same job as nonBtnClick(). Thank you though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.