I want to perform some functionality on resize event of form (or Scene
or Stage
whatever it is).
But how can I detect resize event of form in JavaFX?
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);
Keeping a fixed width to height ratio:
stage.minHeightProperty().bind(stage.widthProperty().multiply(0.5));
stage.maxHeightProperty().bind(stage.widthProperty().multiply(0.5));
Cut the long story short :
container.widthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
if (newValue.floatValue()!=oldValue.floatValue()) resizeKids(newValue);
}
});
Also you may want to check new width/height with the old values for prevent duplication .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.