Search code examples
javajavafxstage

How to dynamically change the height of a stage in JavaFx


Is it possible to create a Stage in JavaFx whose height changes dynamically? Or, in other words, is it possible to change the height of a Stage once it has been created (stage.show()), so that the stage morph's into its new height?


Solution

  • Use stage.sizeToScene() to resize the stage to whatever the current preferred size of it's root is.

     stage.setScene(new Scene(someContent));
     stage.show();
     ....
     stage.getScene().setRoot(someNewContent));
     stage.sizeToScene();
    

    You can also just call stage.setWidth() or stage.setHeight() if you don't need the automatic sizing behavior of sizeToScene().

    You can use a Timeline to animate the stage size change if you need to do that.