0

I have created a JavaFX application that contains two windows. The first window is open by default and has a button that when clicked is supposed to open the second window. I am trying to get the 2nd window stage into my Controller class so I can use the following:

 public void showSubWindow() {
    anotherStage.show();
}

I managed to get the PRIMARY stage (i.e) the first window into my controller class using

    @FXML GridPane pane;
    Stage stage = (Stage) pane.getScene().getWindow();

But when I try to use this same method to get my second window, I get a null pointer exception on this line

    Stage stage = (Stage) panetwo.getScene().getWindow();

How do I get the @FXML take elements from the 2nd FXML file in this particular case and not the first?

  • That code will work fine under the correct circumstances. It's pretty much impossible to answer this question as it stands. Why won't it compile? What have to done to try to fix the compilation error (whatever that error is)? – James_D Mar 25 '17 at 00:11
  • I have managed to make some progress. I will edit my question to reflect that if I cannot figure it out in 30 minutes. – user2778506 Mar 25 '17 at 00:21
  • Well there we go, I edited it. – user2778506 Mar 25 '17 at 00:42
  • You cannot retrieve the scene from a node before adding it to a scene. You're probably doing this in the `initialize` method of the controller which means at this time `panetwo` is not yet part of the scene... – fabian Mar 25 '17 at 07:04

0 Answers0