show JavaFX Confirmation - Java JavaFX

Java examples for JavaFX:Dialog

Description

show JavaFX Confirmation

Demo Code


import java.util.Optional;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;

public class Main{
    public static Optional<ButtonType> showConfirmation(
            final String headerText, final String title,
            final String contentText) {
        return showAlert(headerText, title, contentText,
                AlertType.CONFIRMATION);
    }//from   w  ww  . j  a  va 2 s  .  c o  m
    private static Optional<ButtonType> showAlert(final String headerText,
            final String title, final String contentText,
            final AlertType alertType) {

        ButtonType[] buttons;

        if (alertType.equals(AlertType.CONFIRMATION)) {
            buttons = new ButtonType[2];
            buttons[0] = ButtonType.YES;
            buttons[1] = ButtonType.NO;
        } else {
            buttons = new ButtonType[1];
            buttons[0] = ButtonType.OK;
        }

        Alert alert = new Alert(alertType, contentText, buttons);

        alert.setHeaderText(headerText);
        alert.setTitle(title);

        if (alertType.equals(AlertType.CONFIRMATION))
            return alert.showAndWait();
        else {
            alert.show();
            return Optional.empty();
        }
    }
}

Related Tutorials