You can listen to the changes of the widthProperty
and the heightProperty
of the Stage
:
stage.widthProperty().addListener((obs, oldVal, newVal) -> {
// Do whatever you want
});
stage.heightProperty().addListener((obs, oldVal, newVal) -> {
// Do whatever you want
});
Note: To listen to both width and height changes, the same listener can be used really simply:
ChangeListener<Number> stageSizeListener = (observable, oldValue, newValue) ->
System.out.println("Height: " + stage.getHeight() + " Width: " + stage.getWidth());
stage.widthProperty().addListener(stageSizeListener);
stage.heightProperty().addListener(stageSizeListener);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…