show JavaFX Warning Alert - Java JavaFX

Java examples for JavaFX:Alert

Description

show JavaFX Warning Alert

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 void showWarning(final String headerText,
            final String title, final String contentText) {
        showAlert(headerText, title, contentText, AlertType.WARNING);
    }/* w  w  w.  j  ava  2  s  .c  om*/
    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