Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Display method to return boolean? - JavaFX

I have a class called GUI which manages my application. When the user wants to delete his account in my program, I want a warning box to pop up and demand him to confirm or cancel his action, so he doesn't accidentally delete his account.

 package application;

 import javafx.geometry.Pos;
 import javafx.scene.Scene;
 import javafx.scene.control.Button;
 import javafx.scene.control.Label;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.VBox;
 import javafx.stage.*;

 /**
  * An Alert Box for the GUI
  * to display errors to the user.
  *
  */
public class AlertBox
{   
Stage window;

public boolean display(String title, String message)
{       
    boolean cancel = false;

    window = new Stage();                                                               // Create the stage.        

    window.initModality(Modality.APPLICATION_MODAL);                                    // If window is up, make user handle it.
    window.setTitle(title);
    window.setMinHeight(350);
    window.setMinWidth(250);

    VBox root = new VBox();                 // Root layout.

    Label warning = new Label(message);     // Message explaining what is wrong.

    HBox buttonLayout = new HBox();         // The yes and cancel button.

    Button yesButton = new Button("Yes");   // Yes button for the user.
    Button noButton = new Button("No");     // No button for the user.

    yesButton.setOnAction(e ->
    {
        cancel = false;
    });

    noButton.setOnAction(e ->
    {
        cancel = true;
    });
    buttonLayout.getChildren().addAll(yesButton, noButton);


    root.getChildren().addAll(warning, buttonLayout);
    root.setAlignment(Pos.CENTER);

    Scene scene = new Scene(root);
    window.setScene(scene);
    window.show();
}

/**
 * Closes the window and returns whether the user said yes or no.
 * @param variable
 * @return
 */
private boolean close(boolean variable)
{
    window.close();
    return variable;
}

}

I want my GUI class to know exactly what occurred while the user was in the AlertBox class. Did the user click Yes or No? So I thought about making the display method boolean. Here's where the problem is, my event listener expression cannot return any values because it is inside of a callback of type void. Then I thought, "Oh I'll just make the close method return a boolean". But then I remembered that the original function I called was:

AlertBox warning = new AlertBox;
boolean userWantsToDelete = warning.display("Warning!", "You are about to delete your account. Are you sure you would like to do this?");

which wants the display method to return a variable, NOT the close method. I can't just call close either, as that won't work. What can I do to assist in this problem? Thank you very much.

like image 498
Hatefiend Avatar asked May 11 '26 17:05

Hatefiend


1 Answers

You can use JavaFX Alerts to perform your task easily :

    Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
    alert.setTitle("Confirmation Dialog");
    alert.setHeaderText("Warning !");
    alert.setContentText("Are you sure you want to perform this action ?");

    Optional<ButtonType> result = alert.showAndWait();
    if (result.get() == ButtonType.OK) {
     // delete user
 }

result.get() returns a boolean value. So you can check whether user clicked ButtonType.OK or ButtonType.CANCEL by comparing it inside the if condition.

Here is an example for you to understand : enter image description here

With default Alert you get two buttons one is OK button and the other one is Cancel. But if you wish to customize the Alert you can add your own button types into it like below :

ButtonType buttonTypeOne = new ButtonType("One");
ButtonType buttonTypeTwo = new ButtonType("Two");
ButtonType buttonTypeThree = new ButtonType("Three");

And add them to the alert :

alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree);

So you can check which button was pressed by the user

Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne){
    // ... user chose "One"
} else if (result.get() == buttonTypeTwo) {
    // ... user chose "Two"
} else{
    // ... user chose "Three"
}
like image 159
Madushan Perera Avatar answered May 14 '26 08:05

Madushan Perera