Skip to content
CrossFly is 100% open sourceFollow us on GitHub
CrossFlyCrossFly
Get started

This page was machine-translated from Portuguese and may contain errors. The Portuguese version is canonical.

How to ask for confirmation with Alert

Purpose. Show a modal dialog with a title, a message and one or more buttons, and run an action depending on the button pressed. Use it for destructive confirmations (“Delete?”), warnings that require the user’s acknowledgment, or simple choices.

APIs used

  • new Alert(String title, String message, String... buttonTexts) — creates the alert. The button texts are passed in the constructor (index 0, 1, 2…). If none is given, it uses "OK".
  • alert.buttonAction(int index, Component.Action action) — registers the callback for the button at that index. There is no addButton; the buttons were already declared in the constructor.
  • alert.show() — displays the dialog (inherited from Window).
  • alert.getStyle().fadeBackground(int alpha) — darkens the background behind the alert (0–255). getStyle() returns the alert’s WindowStyle.
  • alert.getButton(int index) — returns the Button at that index (available after show()).

Non-obvious behavior: if you don’t register a buttonAction for a button, it just closes the alert (hide()) when tapped. So, for “Cancel”, you often don’t even need a callback.

Complete example (copy, paste and run)

package com.exemplo;

import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;

/** Runnable main screen. Point the launcher (classname) and run: gradlew :lwjgl3:run */
public class MainWindow extends Window
{
   @Override
   public void onShow() throws Exception
   {
      super.onShow();
      setLayout(new VerticalLayout());

      // Trigger button: opens the Alert when tapped
      Button btn = new Button("Excluir registro");
      btn.onAction(origin ->
      {
         Alert alert = new Alert(
            "Excluir registro",
            "Esta ação não pode ser desfeita. Deseja continuar?",
            "Cancelar",   // index 0
            "Excluir");   // index 1

         // Darkens the background (optional, but recommended for destructive modals)
         alert.getStyle().fadeBackground(150);

         // Button 0 (Cancel) with no callback → just closes.
         // Button 1 (Delete) runs the action and closes:
         alert.buttonAction(1, o ->
         {
            excluirRegistro();
            alert.hide();
         });

         alert.show();
      });
      add(LAY_FILL, btn);
   }

   private void excluirRegistro()
   {
      // ... your deletion logic ...
   }
}

The snippet below shows just the core. To run it, place it in the MainWindow skeleton above.

Minimal snippet

Alert alert = new Alert(
   "Excluir registro",
   "Esta ação não pode ser desfeita. Deseja continuar?",
   "Cancelar",   // index 0
   "Excluir");   // index 1

// Darkens the background (optional, but recommended for destructive modals)
alert.getStyle().fadeBackground(150);

// Button 0 (Cancel) with no callback → just closes.
// Button 1 (Delete) runs the action and closes:
alert.buttonAction(1, origin ->
{
   excluirRegistro();
   alert.hide();
});

alert.show();

Verification note (2026 code). Confirmed in source: constructor Alert(title, message, String... buttonTexts) + buttonAction(int, Action). The button layout (iOS/Material/Flutter) is decided by the active Skin’s AlertStyle — switch the Skin (or use alert.skin(new IOSSkin())) to change the look without changing the logic code. The alert’s width/height is auto-computed from the title, message and buttons.

Dig deeper

Was this page helpful?