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 before a destructive action

Purpose. Before deleting/discarding something, show an Alert with two buttons (e.g., “Cancel” and “Delete”). Only run the destructive action in the confirmation button’s callback.

APIs used

  • new Alert(String title, String message, String... buttonTexts) — creates the dialog. Alert is a Window; the buttons follow the active Skin.
  • Alert.buttonAction(int index, Component.Action action) — sets a button’s action (0-based index). Without an action, the button just closes (hide()).
  • Alert.show() — stacks the dialog (modal push).
  • WindowStyle.fadeBackground(int alpha) — dims the windows below (0–255). Get the style via getStyle().
  • Hint.show(String message, int delayMs) — shows a brief feedback toast.

Complete example (copy, paste, and run)

package com.exemplo;

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

/** Runnable main screen: a button triggers the confirmation Alert. */
public class MainWindow extends Window
{
   @Override
   public void onShow() throws Exception
   {
      super.onShow();
      setLayout(new VerticalLayout());

      add(new Label("Pedido #1234"));
      add(new Button("Excluir pedido").onAction(e -> confirmarExclusao()));
   }

   private void confirmarExclusao()
   {
      Alert alert = new Alert("Excluir pedido", "Esta ação não pode ser desfeita.", "Cancelar", "Excluir");

      // Darkens the background (0..255). The Alert's getStyle() is a WindowStyle.
      ((WindowStyle) alert.getStyle()).fadeBackground(150);

      // Index 0 = "Cancel": with no buttonAction, the button already closes the Alert on its own.
      // Index 1 = "Delete": destructive action + feedback.
      alert.buttonAction(1, e ->
      {
         excluirPedido();                 // destructive action
         alert.hide();                    // closes the dialog
         Hint.show("Pedido excluído", 1500);
      });

      alert.show();
   }

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

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

Minimal snippet

// verified in the source: Alert.buttonAction(idx, Action); WindowStyle.fadeBackground(int 0-255); Hint.show(msg, ms)
Alert alert = new Alert("Excluir pedido", "Esta ação não pode ser desfeita.", "Cancelar", "Excluir");

// Darkens the background (0..255). The Alert's getStyle() is a WindowStyle.
((WindowStyle) alert.getStyle()).fadeBackground(150);

// Index 0 = "Cancel": with no buttonAction, the button already closes the Alert on its own.
// Index 1 = "Delete": destructive action + feedback.
alert.buttonAction(1, e ->
{
   excluirPedido();                 // destructive action
   alert.hide();                    // closes the dialog
   Hint.show("Pedido excluído", 1500);
});

alert.show();

Details that avoid surprises:

  • If you do not set buttonAction for a button, the Alert associates onAction(e -> hide()) — the button closes the dialog and nothing more. That’s why “Cancel” usually doesn’t need buttonAction.
  • Always put the destructive action in the confirmation button (here, index 1), never as the default behavior.
  • fadeBackground takes an alpha of 0–255 (not 0xAARRGGBB). 150 gives a noticeable dimming without hiding the content.

Go deeper

Was this page helpful?