Nadeeja
Nadeeja

Reputation: 121

How to set window size on runtime?

I'm developing a JavaFX FXML application. I need to resize the window from the controller class at runtime.

I found out that its possible to do this from the application class by setting maxHeight and maxWidth properties of the stage. But how to do it from the controller class while the application is running?

Upvotes: 1

Views: 1967

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49185

Define a button in the controller class and set on action of it like

btn.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        // OR, if you define btn as @FXML private Button btn.
        Stage stage = (Stage) btn.getScene().getWindow();
        // these two of them return the same stage
        stage.setWidth(new_val);
        stage.setHeight(new_val);
    }
});

where stage is your primary (main) stage.

Upvotes: 4

Related Questions