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?
In JavaFX applications, events are notifications that something has happened. As a user clicks a button, presses a key, moves a mouse, or performs other actions, events are dispatched. Registered event filters and event handlers within the application receive the event and provide a response.
The show() method returns immediately regardless of the modality of the stage. Use the showAndWait() method if you need to block the caller until the modal stage is hidden (closed). The modality must be initialized before the stage is made visible.
Showing a Stage The difference between the JavaFX Stage methods show() and showAndWait() is, that show() makes the Stage visible and the exits the show() method immediately, whereas the showAndWait() shows the Stage object and then blocks (stays inside the showAndWait() method) until the Stage is closed.
You can do it with stage. setResizable(false); You can also remove window buttons with stage.
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));
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