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.Alertis aWindow; the buttons follow the activeSkin.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 viagetStyle().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
MainWindowskeleton 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
buttonActionfor a button, the Alert associatesonAction(e -> hide())— the button closes the dialog and nothing more. That’s why “Cancel” usually doesn’t needbuttonAction. - Always put the destructive action in the confirmation button (here, index 1), never as the default behavior.
fadeBackgroundtakes an alpha of 0–255 (not 0xAARRGGBB). 150 gives a noticeable dimming without hiding the content.
Go deeper
- Dialogs and modals
- Sibling recipe: Open and close a screen
Was this page helpful?Thanks for the feedback!