I have been trying to set the scene's width and height outside of the constructor and it's been to no avail. After looking through the Scene
API I saw a method that lets you get the height and width respectively but not one to set the method.. :s (design flaw maybe).
After further research I came across the SceneBuilder
and found methods that could modify the height and width. However, I do not know how to apply it to a scene object already created or how to create a SceneBuilder
object that could be used in place of the scene object.
Once you created Scene
and assigned it to the Stage
you can use Stage.setWidth
and Stage.setHeight
to change both stage and scene sizes simultaneously.
SceneBuilder
can't be applied to an already created object, it can only be used for the scene creation.
It seems not possible to set the size of the Scene
after it has been created.
Setting the size of the Stage
means to set the size of the window, which includes the size of the decoration. So the Scene
is smaller in size, unless the Stage
is undecorated.
My solution is to compute the size of the decoration while initialization and add it to the size of the Stage
when resizing:
private Stage stage;
private double decorationWidth;
private double decorationHeight;
public void start(Stage stage) throws Exception {
this.stage = stage;
final double initialSceneWidth = 720;
final double initialSceneHeight = 640;
final Parent root = createRoot();
final Scene scene = new Scene(root, initialSceneWidth, initialSceneHeight);
this.stage.setScene(scene);
this.stage.show();
this.decorationWidth = initialSceneWidth - scene.getWidth();
this.decorationHeight = initialSceneHeight - scene.getHeight();
}
public void resizeScene(double width, double height) {
this.stage.setWidth(width + this.decorationWidth);
this.stage.setHeight(height + this.decorationHeight);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With