Kannagara
Kannagara

Reputation: 23

How to disable/remove stage close button only

I've tried using primaryStage.initStyle(StageStyle.UNDECORATED); but it makes the window without a header. What I wanted is just to remove/disable the close button is it possible?

Upvotes: 1

Views: 3790

Answers (2)

Ghostmerc
Ghostmerc

Reputation: 36

I know this is coming late but i'll just build on Sam Sun's answer. I had the same issue but I followed his advice and consumed the event by:

primaryStage.setOnCloseRequest(event -> event.consume());

That did it for me. You can even go ahead and display an alert telling the user that the window cannot be closed like so:

primaryStage.setOnCloseRequest(event -> {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setContentText("This window cannot be closed");
        alert.showAndWait();
        event.consume();
});

I hope this helps anyone who gets the same issue.

Upvotes: 0

samczsun
samczsun

Reputation: 1024

You can't actually remove this button. The best alternative is to simply disable it by consuming the close event.

If your user is tempted to use the close button, then that typically means you don't give them an obvious alternative of closing the window. Perhaps add a button for Save or OK?

Upvotes: 2

Related Questions