Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen resize event of Stage in JavaFX?

Tags:

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?

like image 978
Amita Patil Avatar asked Jul 06 '16 04:07

Amita Patil


People also ask

How JavaFX handle events?

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.

Which method is used to make a JavaFX stage visible?

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.

How do you display a stage in JavaFX?

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.

How do I stop windows from resizing JavaFX?

You can do it with stage. setResizable(false); You can also remove window buttons with stage.


2 Answers

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);  
like image 78
DVarga Avatar answered Sep 29 '22 21:09

DVarga


Keeping a fixed width to height ratio:

stage.minHeightProperty().bind(stage.widthProperty().multiply(0.5)); stage.maxHeightProperty().bind(stage.widthProperty().multiply(0.5)); 
like image 44
Miss Chanandler Bong Avatar answered Sep 29 '22 19:09

Miss Chanandler Bong