3

I'm running into a bit of a problem. I'm creating a program for a client. In the program, I have implement a dedicated 'close/shut down' button - which requires a password in order to properly shut down. But an alternative (and less safe) way of closing the program is by hitting the red close or 'X' button: top right (Windows) or top left(Mac).

I do not want the red x button to actually close the entire program. What I would like to know: is it possible to completely disable the red 'x' button from closing the entire program? If possible, could someone provide code for this?

What I'm using: IntelliJ IDEA (Ultimate), JavaFX with Java 8, Dev. Language: Java

RBT
  • 24,161
  • 21
  • 159
  • 240
Yuri Khechoyan
  • 103
  • 1
  • 2
  • 4
  • "I do not want the red x button to actually close the entire program". Can you clarify what you want here? You want it to still close the window, but you want the program to continue running when the window is closed? You simply need `Platform.setImplicitExit(false);` if that's what you mean. – James_D Jan 02 '17 at 04:58

1 Answers1

8

Add a event handler to the onCloseRequest event of the stage. This allows you to prevent the window from closing by consuming the event and executing your own shutdown procedure instead:

private void shutdown(Stage mainWindow) {
    // you could also use your logout window / whatever here instead
    Alert alert = new Alert(Alert.AlertType.NONE, "Really close the stage?", ButtonType.YES, ButtonType.NO);
    if (alert.showAndWait().orElse(ButtonType.NO) == ButtonType.YES) {
        // you may need to close other windows or replace this with Platform.exit();
        mainWindow.close();
    }
}

@Override
public void start(Stage primaryStage) {
    primaryStage.setOnCloseRequest(evt -> {
        // prevent window from closing
        evt.consume();

        // execute own shutdown procedure
        shutdown(primaryStage);
    });

    StackPane root = new StackPane();

    Scene scene = new Scene(root, 100, 100);

    primaryStage.setScene(scene);
    primaryStage.show();
}
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Again, I already have a dedicated shut down button that leads to a new window (stage) that asks the person to enter a 'Shut down Code' - issued by a button that is setOnAction(ActionEvent e) ->{}. Only the employees will know this information. What I want is that if a customer accidentally (or intentionally) presses the red X, it leads them to the same location as the implemented 'Shut down' would. I don't want to duplicate the stage, scene, etc. I just want the program to do this: red x is pressed?.. go to setOnAction(ActionEvent e) -> that handles the manual shut down procedure. – Yuri Khechoyan Jan 02 '17 at 16:49
  • @YuriKhechoyan And you write that comment because??? Whether you show a `Alert` or start some other procedure to allow the user to shut down the program is entirely up to you. You could also replace the scene and instead show some logout screen, if that's how you want to deal with this... – fabian Jan 02 '17 at 17:09
  • It works!! Thank you again! I just had to do a little tinkering with some of the code that you have provided! Much appreciated! – Yuri Khechoyan Jan 02 '17 at 17:31
  • How do I make something happen to a certain element in a queue? For an example: if someone is 100th in the queue = 'System.out.println("Congratulations! You are the 100th Customer!");' – Yuri Khechoyan Jan 02 '17 at 20:51
  • @YuriKhechoyan please don't ask new, unrelated questions in comments, instead ask them as new questions. – jewelsea Jan 03 '17 at 18:45