AlertUtil
Alert dialogs with fully self-contained, dependency-free theming. Plain
JavaFX Alerts always render light, which clashes with dark or
branded UIs. AlertUtil fixes this without shipping any CSS
file: an AlertTheme is rendered to CSS in code and
injected through a base64 data: URI, restyling background,
text, header and buttons (including hover).
Diálogos de alerta con theming totalmente autónomo y sin dependencias. Los
Alert de JavaFX siempre se renderizan en claro, lo que choca con
interfaces oscuras o de marca. AlertUtil lo resuelve sin enviar
ningún archivo CSS: un AlertTheme se renderiza a CSS en
código y se inyecta mediante un data: URI en base64,
reestilizando fondo, texto, cabecera y botones (incluido el hover).
Three levels of customizationTres niveles de personalización
- Built-in presets —
AlertTheme.light()andAlertTheme.dark(), zero config.Presets integrados —AlertTheme.light()yAlertTheme.dark(), sin configuración. - Custom colors —
AlertTheme.builder()sets any colors, font family and size with nativeColors.Colores personalizados —AlertTheme.builder()fija cualquier color, familia y tamaño de fuente conColors nativos. - Raw CSS —
showStyled(...)takes an arbitrary stylesheet string for total control.CSS en crudo —showStyled(...)acepta una cadena de hoja de estilo arbitraria para control total.
Set a default once via setDefaultTheme(...) and every alert
adopts it; per-call overloads still let you override for a single dialog.
Fija un valor por defecto una vez con setDefaultTheme(...) y cada
alerta lo adopta; las sobrecargas por llamada te siguen permitiendo
sobreescribirlo para un solo diálogo.
Quick startInicio rápido
AlertUtil alerts = AlertUtil.getInstance();
alerts.setDefaultTheme(AlertUtil.AlertTheme.dark()); // whole app goes dark
alerts.show(AlertType.INFORMATION, "Saved", "Your changes were saved.");
boolean ok = alerts.showConfirmation("Delete?", owner, "This cannot be undone.");
if (ok) { /* proceed */ }
boolean yes = alerts.askYesNo("Quit", owner, "Exit the application?");
Default themeTema por defecto
| MethodMétodo | PurposePropósito |
|---|---|
setDefaultTheme(AlertTheme) | Theme applied to alerts shown without an explicit one (null → native look).Tema aplicado a las alertas mostradas sin uno explícito (null → aspecto nativo). |
getDefaultTheme() | The current default, or null.El valor por defecto actual, o null. |
clearDefaultTheme() | Reverts un-themed alerts to the native look.Revierte las alertas sin tema al aspecto nativo. |
Non-blocking alertsAlertas no bloqueantes
Shows a non-blocking alert (uses the default theme when none is given). type must be non-null.Muestra una alerta no bloqueante (usa el tema por defecto cuando no se da uno). type no puede ser null.
Blocking (modal) alertsAlertas bloqueantes (modales)
Owned, modal, waits for close (showAndWait).Con owner, modal, espera el cierre (showAndWait).
OK / Cancel dialog. Returns true only if OK was clicked.Diálogo Aceptar / Cancelar. Devuelve true solo si se pulsó Aceptar.
Yes / No dialog. Returns true only if Yes was clicked.Diálogo Sí / No. Devuelve true solo si se pulsó Sí.
Raw-CSS escape hatchVía de escape con CSS en crudo
Non-blocking alert styled with an arbitrary CSS string (injected via base64 data URI). css must be non-blank.Alerta no bloqueante estilizada con una cadena de CSS arbitraria (inyectada vía data URI en base64). css no puede estar en blanco.
Building a custom AlertThemeConstruir un AlertTheme personalizado
AlertUtil.AlertTheme is an immutable theme built with a
fluent Builder. Every field defaults to the light theme, so
you only set what you want to change.
AlertUtil.AlertTheme es un tema inmutable construido con un
Builder fluido. Cada campo usa por defecto el tema claro, así que
solo defines lo que quieres cambiar.
AlertUtil.AlertTheme brand = AlertUtil.AlertTheme.builder()
.background(Color.web("#1b1e2b"))
.text(Color.web("#e6e6e6"))
.buttonBackground(Color.web("#2370ed"))
.buttonText(Color.WHITE)
.buttonHover(Color.web("#1f9bcf"))
.fontFamily("Segoe UI")
.fontSize(14)
.build();
alerts.show(AlertType.WARNING, "Heads up", "Low disk space.", brand);
| Builder setterSetter del builder | AffectsAfecta a |
|---|---|
background / border | Dialog pane fill and 1px border.Relleno del panel del diálogo y borde de 1px. |
text | Content text color.Color del texto del contenido. |
headerBackground / headerText | Header panel (when present).Panel de cabecera (cuando está presente). |
buttonBackground / buttonText / buttonHover | Buttons and their hover state.Botones y su estado hover. |
fontFamily (null/blank = inherit) · fontSize (> 0) | Typography.Tipografía. |
Styling applies to the dialog's content area (the DialogPane). The surrounding window title bar is drawn by the operating system and is outside JavaFX's reach.El estilo se aplica al área de contenido del diálogo (el DialogPane). La barra de título de la ventana que lo rodea la dibuja el sistema operativo y está fuera del alcance de JavaFX.
showModal, showConfirmation and askYesNo block and must be invoked on the JavaFX Application Thread, as JavaFX requires.showModal, showConfirmation y askYesNo bloquean y deben invocarse en el JavaFX Application Thread, como exige JavaFX.
RecommendationsRecomendaciones
Prefer AlertUtil over Message for new code when you want theming. Set a default theme once at startup so the whole app is consistent.Prefiere AlertUtil sobre Message en código nuevo cuando quieras theming. Fija un tema por defecto una vez al inicio para que toda la app sea consistente.
Use askYesNo for destructive confirmations (clearer than OK/Cancel), and pass the owning Window so the dialog centers over it.Usa askYesNo para confirmaciones destructivas (más claro que Aceptar/Cancelar), y pasa la Window propietaria para que el diálogo se centre sobre ella.