dud3
dud3

Reputation: 145

How to set default close operation in JavaFX?

When I press the red close button, I want the window not to close on JavaFX.

Any suggestions?

Upvotes: 2

Views: 647

Answers (1)

James_D
James_D

Reputation: 209603

From the Javadocs for Window.setOnCloseRequest():

Called when there is an external request to close this Window. The installed event handler can prevent window closing by consuming the received event.

So all you need is

stage.setOnCloseRequest(Event::consume);

or, if you are going to perform other actions as well:

stage.setOnCloseRequest(event -> {
    // do some stuff...
    event.consume();
});

Upvotes: 8

Related Questions