How to listen resize event of Stage in JavaFX?

风格不统一 提交于 2020-01-09 07:41:04

问题


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?


回答1:


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); 



回答2:


Keeping a fixed width to height ratio:

stage.minHeightProperty().bind(stage.widthProperty().multiply(0.5));
stage.maxHeightProperty().bind(stage.widthProperty().multiply(0.5));


来源:https://stackoverflow.com/questions/38216268/how-to-listen-resize-event-of-stage-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!