Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+10 votes

I have created an alert type WARNING to be displayed after the user clicks a button. The purpose of this warning is to notify the user that this action will result in a loss of progress. However, I cannot figure out how to use the warning to abort the action, and right now, after the user closes or hits "ok" on the warning it still navigates away from out "edit" screen. Any help with Alerts would be appreciated.

asked in CSC305 Fall 2023 by (1 point)

2 Answers

+6 votes

You could follow this code and modify to suit your needs:
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);


// Show the alert and wait for the user's response
alert.showAndWait().ifPresent(response -> {
    if (response == ButtonType.OK) {
        // User clicked OK, navigate away from the edit screen
    } else {
        // User clicked Cancel or closed the alert, do nothing

I hope this helps.

answered by (2.1k points)

Is this something we can do for other Alert types? For example, if I had a pop-up that might ask if a user wants to perform one of two options, but not necessarily abort an action, would this still work?