-1

I have two scenes A and B and a stage.

If I'm in scene A, and change the stage width / height, it will update the UI to scale it to the new stage dimensions.

However if from there I switch to scene B, the UI in scene B will not be updated till I force a update by doing something like resizing the screen or opening and closing the stage.

Is there a way to update all scenes when the stage width / height is changed? Since only one scene can be set to the stage at one time the current changes are not applying automatically to all scenes.

Here's a minimal reproduced example.

    @Override
    public void start(Stage stage) throws Exception {
        
          stage.setWidth(400);
          stage.setHeight(400);
        
          Scene scene1, scene2;
          
          BorderPane layout1 = new BorderPane();
          Button buttontosecond = new Button("go to second page");
          layout1.setCenter(buttontosecond);
          scene1 = new Scene(layout1, stage.getWidth(), stage.getHeight());
          
        
          BorderPane layout2 = new BorderPane();
          Button buttonsize = new Button("change size");   
          Button buttontomain = new Button("back to main");
          VBox vbox = new VBox(buttonsize, buttontomain);
          
          
          vbox.setAlignment(Pos.CENTER);

          
          layout2.setCenter(vbox);
          scene2 = new Scene(layout2, stage.getWidth(), stage.getHeight());
          
          buttontomain.setOnAction(actionEvent -> {stage.setScene(scene1);});
          buttontosecond.setOnAction(actionEvent -> {stage.setScene(scene2);});
          buttonsize.setOnAction(actionEvent -> {stage.setWidth(200);stage.setHeight(200);});


          stage.setScene(scene1);
          stage.show();

    }


Tom
  • 1,235
  • 9
  • 22
  • Can you create a [mre]? – Matt Mar 16 '21 at 15:28
  • @Matt here it is – Tom Mar 16 '21 at 15:45
  • Im not sure if I understand correctly I start up the app change the size then swap scene's. Here is where im getting confused when scene B loads for me it loads the same size as scene A and everything is Centered still. Reading through your post it looks like here yours is no longer centerd? – Matt Mar 16 '21 at 15:59
  • @Matt Yes that's correct, its no longer centered and I have no idea why – Tom Mar 16 '21 at 16:01
  • What version of Java are you on? What is your OS? Are you using multiple monitors? – Matt Mar 16 '21 at 16:04
  • 1
    @Matt I have multiple monitors. After running the program on the other monitor everything ended up working fine... thanks for the suggestion – Tom Mar 16 '21 at 16:07
  • curious: why do you switch scenes (vs. switching the scene's root)? – kleopatra Mar 16 '21 at 16:35
  • @kleopatra is that not how stages and scenes were intended to be used? I.e. 1 stage, multiple scenes, change the scene via stage.setScene() – Tom Mar 16 '21 at 16:39
  • still curious: why should it or where did you get that idea from? – kleopatra Mar 16 '21 at 16:44
  • @kleopatra many examples on the internet. – Tom Mar 16 '21 at 16:47

1 Answers1

0

DO not do scene switching in this way. Instead implement a single scene root-switching system as shown in the answer to this post. I did not manage to get the code above to work, but I found that the reason for this was my dual monitor setup. When dragging the game window into the other monitor it worked fine.

Tom
  • 1,235
  • 9
  • 22