Mostrar Alerts en JavaFX

Hola a todos, hoy os explicare como mostrar Alerts en JavaFX.

Tanto en JavaFX como en Swing, necesitamos mostrar alertas al usuario, ya sea para informar o alertar del estado de la aplicación.

En JavaFX, es algo diferente de hacer, se tiene que usar una clase llamada Alert.

 

Los tipos de alertas que hay son:

  • ERROR
  • INFORMATION
  • WARNING
  • CONFIRMATION

Veamos un pequeño ejemplo:


    @FXML
    private void mostrarAlertError(ActionEvent event) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setHeaderText(null);
        alert.setTitle("Error");
        alert.setContentText("Error en la aplicacion");
        alert.showAndWait();
    }

    @FXML
    private void mostrarAlertInfo(ActionEvent event) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText(null);
        alert.setTitle("Info");
        alert.setContentText("Informacion sobre la aplicación");
        alert.showAndWait();
    }

    @FXML
    private void mostrarAlertWarning(ActionEvent event) {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setHeaderText(null);
        alert.setTitle("Info");
        alert.setContentText("Warning en la aplica");
        alert.showAndWait();
    }

    @FXML
    private void mostrarAlertConfirmation(ActionEvent event) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setHeaderText(null);
        alert.setTitle("Confirmacion");
        alert.setContentText("¿Deseas realmente confirmar?");
        alert.showAndWait();
    }

    @FXML
    private void mostrarAlertCabecera(ActionEvent event) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("Cabecera");
        alert.setTitle("Info");
        alert.setContentText("Informacion sobre la aplicación");
        alert.showAndWait();
    }

Seguramente, os preguntéis, que es lo de alert.setHeaderText, esto lo que hace es ponernos una cabecera en el alert.

Este es el resultado final:

¿Quieres descargar el proyecto? Aquí lo tienes.

Espero que os sea de ayuda. Si tenéis dudas, preguntad. Estamos para ayudarte.

Compartir

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *