javafx on window resize code example

Example 1: javafx event resize

Stage stage = new Stage();

stage.widthProperty().addListener((obs, oldVal, newVal) -> {
  //on change width
});

stage.heightProperty().addListener((obs, oldVal, newVal) -> {
  //on change height
});

Example 2: javafx resizable window

scene.widthProperty().addListener(new ChangeListener<Number>() {
    @Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth, Number newSceneWidth) {
        System.out.println("Width: " + newSceneWidth);
    }
});
scene.heightProperty().addListener(new ChangeListener<Number>() {
    @Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
        System.out.println("Height: " + newSceneHeight);
    }
});

Tags:

Java Example