Josh Beaver
Josh Beaver

Reputation: 127

JavaFX - Button to minimize the stage

I have an undecorated stage in a JavaFX Application. In order to minimize it I need a minimize button. I created my layout in an fxml document which has a minimize button, but when I try to minimize using the action listener for this button located inside the controller using stage.setIconified(true), it cannot find stage.

How can I find a reference to the stage in the controller class?

Upvotes: 4

Views: 8679

Answers (3)

L A R R
L A R R

Reputation: 1

Use this Stage stage = (Stage) minimize.getScene().getWindow(); stage.setIconified(true); // minimize can be any element in that scene

Upvotes: 0

Juce
Juce

Reputation: 341

Solution number 1 is what ItachiUchiha posted.

Solution number 2 is just creating a setter-method in the controller and give the controller the stage itself.

Upvotes: 0

ItachiUchiha
ItachiUchiha

Reputation: 36792

You can try :

button.setOnAction(e -> {
    ((Stage)((Button)e.getSource()).getScene().getWindow()).setIconified(true);
});

Upvotes: 11

Related Questions