Skip to main content

JAlert

A flexible alert component for displaying important messages, notifications, and status updates to users.

Basic Usage

import com.jjarroyo.components.JAlert;
import com.jjarroyo.components.JIcon;

// Simple alert with title only
JAlert alert = new JAlert("Operation successful!");

// Alert with title and description
JAlert detailedAlert = new JAlert(
    "Account Created",
    "Your account has been successfully created. Check your email for verification."
);

Alert Variants

// Success alert
JAlert success = new JAlert("Success!", "Your changes have been saved.")
    .addClass("alert-success")
    .setIcon(JIcon.CHECK_CIRCLE);

// Error alert
JAlert error = new JAlert("Error!", "Something went wrong. Please try again.")
    .addClass("alert-danger")
    .setIcon(JIcon.ERROR);

// Warning alert
JAlert warning = new JAlert("Warning!", "This action cannot be undone.")
    .addClass("alert-warning")
    .setIcon(JIcon.WARNING);

// Info alert
JAlert info = new JAlert("Info", "New features are available in this version.")
    .addClass("alert-info")
    .setIcon(JIcon.INFO);

With Icons

// Using JIcon enum
JAlert alert = new JAlert("Upload Complete")
    .setIcon(JIcon.CLOUD_UPLOAD)
    .addClass("alert-success");

// Using custom SVG node
SVGPath customIcon = new SVGPath();
customIcon.setContent("M12 2L2 7v10l10 5 10-5V7L12 2z");
JAlert customAlert = new JAlert("Custom Icon")
    .setIcon(customIcon);

Dismissible Alerts

VBox container = new VBox(10);

JAlert dismissible = new JAlert(
    "Cookie Notice",
    "This website uses cookies to improve your experience."
)
.addClass("alert-info")
.setIcon(JIcon.INFO)
.setDismissible(true); // Adds close button

container.getChildren().add(dismissible);

// Alert will hide itself when dismissed
// You can also handle manual removal:
dismissible.setOnMouseClicked(e -> {
    if (dismissed) {
        container.getChildren().remove(dismissible);
    }
});

Complete Examples

Form Validation Feedback

VBox form = new VBox(15);

// Success alert (initially hidden)
JAlert successAlert = new JAlert(
    "Form Submitted",
    "Your information has been successfully saved."
)
.addClass("alert-success")
.setIcon(JIcon.CHECK_CIRCLE)
.setDismissible(true);
successAlert.setVisible(false);
successAlert.setManaged(false);

// Error alert (initially hidden)
JAlert errorAlert = new JAlert(
    "Validation Error",
    "Please fill in all required fields."
)
.addClass("alert-danger")
.setIcon(JIcon.ERROR)
.setDismissible(true);
errorAlert.setVisible(false);
errorAlert.setManaged(false);

JInput nameInput = new JInput("Enter your name");
JButton submitBtn = new JButton("Submit").addClass("btn-primary");

submitBtn.setOnAction(e -> {
    if (nameInput.getText().isEmpty()) {
        // Show error
        errorAlert.setVisible(true);
        errorAlert.setManaged(true);
        successAlert.setVisible(false);
        successAlert.setManaged(false);
    } else {
        // Show success
        successAlert.setVisible(true);
        successAlert.setManaged(true);
        errorAlert.setVisible(false);
        errorAlert.setManaged(false);
    }
});

form.getChildren().addAll(
    errorAlert,
    successAlert,
    nameInput.createWithLabel("Name", true),
    submitBtn
);

System Notifications Panel

VBox notificationPanel = new VBox(10);
notificationPanel.setPadding(new Insets(20));

// Multiple notifications
JAlert notification1 = new JAlert(
    "System Update Available",
    "Version 2.0.1 is ready to install."
)
.addClass("alert-info")
.setIcon(JIcon.DOWNLOAD)
.setDismissible(true);

JAlert notification2 = new JAlert(
    "Low Disk Space",
    "You have less than 10% disk space remaining."
)
.addClass("alert-warning")
.setIcon(JIcon.WARNING)
.setDismissible(true);

JAlert notification3 = new JAlert(
    "Backup Complete",
    "Your data has been successfully backed up."
)
.addClass("alert-success")
.setIcon(JIcon.CHECK_CIRCLE)
.setDismissible(true);

notificationPanel.getChildren().addAll(
    notification1,
    notification2,
    notification3
);

Inline Alert in Card

JCard card = new JCard();
card.setTitle("Delete Account");

VBox content = new VBox(15);

JAlert warningAlert = new JAlert(
    "Warning: This action is permanent",
    "Deleting your account will remove all your data and cannot be undone."
)
.addClass("alert-danger")
.setIcon(JIcon.WARNING_CIRCLE);

JButton deleteBtn = new JButton("Delete My Account")
    .addClass("btn-danger");

JButton cancelBtn = new JButton("Cancel")
    .addClass("btn-secondary");

HBox actions = new HBox(10, cancelBtn, deleteBtn);

content.getChildren().addAll(warningAlert, actions);
card.setBody(content);

API Reference

Constructors

JAlert(String title)
constructor
Creates an alert with only a title
JAlert(String title, String description)
constructor
Creates an alert with title and description

Methods

setIcon(Node icon)
JAlert
Sets a custom icon node (like SVGPath)
SVGPath icon = new SVGPath();
icon.setContent("M12 2L2 7l10 5 10-5-10-5z");
alert.setIcon(icon);
setIcon(JIcon iconEnum)
JAlert
Sets an icon using the JIcon enum
alert.setIcon(JIcon.CHECK_CIRCLE);
setDismissible(boolean dismissible)
JAlert
Enables or disables the close button. When dismissed, the alert hides itself (setVisible(false) and setManaged(false)).
alert.setDismissible(true);
addClass(String... styleClasses)
JAlert
Adds custom CSS style classes to the alert
alert.addClass("alert-success", "custom-class");

CSS Style Classes

Alerts use the following CSS classes:
  • alert - Base alert container
  • alert-success - Success/positive variant (green)
  • alert-danger - Error/danger variant (red)
  • alert-warning - Warning variant (yellow)
  • alert-info - Info variant (blue)
  • alert-icon-container - Icon wrapper
  • alert-icon - Icon element
  • alert-title - Title text
  • alert-description - Description text
  • alert-dismiss-container - Close button container
  • alert-dismiss-btn - Close button
  • alert-close-icon - Close icon (X)

Alert Types Guide

Success

Use for successful operations, confirmations, and positive feedback.Examples: Form submitted, file uploaded, settings saved

Error

Use for errors, failed operations, and critical issues.Examples: Invalid input, server error, operation failed

Warning

Use for warnings, cautions, and important notices.Examples: Unsaved changes, destructive action, expiring soon

Info

Use for informational messages and helpful tips.Examples: New features, tips, general notifications

Best Practices

Alert titles should be brief (5-10 words). Use the description for additional details if needed.
Always pair alerts with relevant icons to improve scannability and accessibility.
Allow users to dismiss informational alerts, but consider keeping critical errors visible until resolved.
Place alerts near the content they relate to. For global messages, position at the top of the interface.
Too many alerts can overwhelm users. Use them sparingly for truly important information.

Accessibility

  • Alerts use semantic colors that work in both light and dark themes
  • Icons provide visual reinforcement of the message type
  • Text wrapping ensures descriptions remain readable
  • Dismissible alerts provide clear close buttons
  • High contrast between text and background for readability

Build docs developers (and LLMs) love