Como abrir um menu de ações (ActionSheet)
Propósito. Mostrar uma lista de ações contextuais em um cartão arredondado na base da tela, com um botão “Cancelar” separado abaixo (padrão iOS). Use quando o usuário precisa escolher entre várias ações relacionadas a um item (“Compartilhar”, “Excluir”…).
APIs usadas
new ActionSheet(String cancelText, String... buttonTexts)— cria a folha. O primeiro argumento é o texto do Cancelar; os demais são os botões de opção (índice 0, 1, 2…).sheet.buttonAction(int index, Component.Action action)— callback do botão de opção daquele índice.sheet.cancelAction(Component.Action action)— callback do botão Cancelar (opcional; por padrão só fecha).sheet.buttonStyle(int index, ComponentStyle style)— estilo por botão (ex.: pintar “Excluir” de vermelho).sheet.buttonStyle(ComponentStyle style)— estilo default para todos os botões de opção.sheet.cancelStyle(ComponentStyle style)/sheet.containerStyle(ComponentStyle style)— estilos do Cancelar e do cartão container.sheet.maxColumns(int cols)— número máximo de colunas quando há muitos botões (default 1; só faz efeito acima de 1).ActionSheet.createDefaultButtonStyle()— estilo base pronto (fundo branco, texto azul,pressable) para você copiar e modificar.sheet.show()— exibe (herdado deWindow).
Comportamento não óbvio: como no
Alert, botões sembuttonActionapenas fecham a folha ao serem tocados. O Cancelar também fecha por padrão.
Exemplo completo (copiar, colar e rodar)
package com.exemplo;
import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;
/** Tela principal executável. Aponte o launcher (classname) e rode: gradlew :lwjgl3:run */
public class MainWindow extends Window
{
@Override
public void onShow() throws Exception
{
super.onShow();
setLayout(new VerticalLayout());
// Botão-gatilho: abre a ActionSheet ao ser tocado
Button btn = new Button("Opções");
btn.onAction(origin ->
{
ActionSheet sheet = new ActionSheet(
"Cancelar", // texto do botão Cancelar (1º argumento)
"Compartilhar", // índice 0
"Excluir"); // índice 1
sheet.buttonAction(0, o ->
{
compartilhar();
sheet.hide();
});
// Destacar a ação destrutiva em vermelho
sheet.buttonStyle(1, ActionSheet.createDefaultButtonStyle().textColor(0xFFFF0000));
sheet.buttonAction(1, o ->
{
excluir();
sheet.hide();
});
sheet.show();
});
add(LAY_FILL, btn);
}
private void compartilhar()
{
// ... sua lógica de compartilhamento ...
}
private void excluir()
{
// ... sua lógica de exclusão ...
}
}
O trecho abaixo mostra só o miolo. Para rodar, coloque-o no esqueleto
MainWindowacima.
Trecho mínimo
ActionSheet sheet = new ActionSheet(
"Cancelar", // texto do botão Cancelar (1º argumento)
"Compartilhar", // índice 0
"Excluir"); // índice 1
sheet.buttonAction(0, origin ->
{
compartilhar();
sheet.hide();
});
// Destacar a ação destrutiva em vermelho
sheet.buttonStyle(1, ActionSheet.createDefaultButtonStyle().textColor(0xFFFF0000));
sheet.buttonAction(1, origin ->
{
excluir();
sheet.hide();
});
sheet.show();
Nota de verificação (código 2026). Confirmado na fonte:
ActionSheet(cancelText, String... buttonTexts),buttonAction(int, Action),cancelAction(Action), e existembuttonStyle(int, ComponentStyle),buttonStyle(ComponentStyle),maxColumns(int). O visual default (cartão iOS) é aplicado independentemente do Skin, masdefaultStyleCreate()usagetSkin().actionSheet()para a janela. A largura padrão é ~92% da tela (widthRatio ≈ .92f).
Aprofunde-se
- Página do widget: ActionSheet · Fonte canônica: Capítulo 16
- Receitas irmãs: Confirmar com Alert · Mostrar um Hint