Make a modal popup
A modal popup asks a question and keeps the user on it until they answer. This guide builds a confirmation popup that dims the screen behind it, blocks it, and runs an action when the user confirms.
What you will build
Section titled “What you will build”Before you start
Section titled “Before you start”- A UI that opens on a backend: UIs.
- What a popup is and how UIs stack: Overlays and popups.
- Click listeners: Events.
1. Start from a confirmation popup
Section titled “1. Start from a confirmation popup”Start from the popup of Open a popup above a screen: a UI marked with UIDataPopup and UIData, whose card asks a question with two buttons.
Change its question to “Delete the file?”, its second button to Delete, and give the popup the action to run in its constructor, so the screen that opens it decides what Delete does:
@UIData(background = true)@UIDataPopup(active = true)public class PopupGuideConfirmPopup extends UI {
private static final Color TEXT = new Color(231, 233, 238).lock(); private static final Color ELEMENT = new Color(38, 43, 53).lock(); private static final Color SURFACE = new Color(23, 26, 33).lock(); private static final Color ACCENT = new Color(91, 140, 255).lock(); private static final Color ON_ACCENT = new Color(14, 16, 20).lock();
private final Runnable action;
public PopupGuideConfirmPopup(final Runnable action) { this.action = action; }Full file: PopupGuideConfirmPopup.java
Check: the class compiles.
2. Open it from the screen
Section titled “2. Open it from the screen”The screen shows a file name held in a StringSignal.
Its Delete button opens the popup with JOID.open and passes the action, which clears the name:
final StringSignal file = new StringSignal("report.pdf");
RectNode.create(560, 290, 800, 500).color(PopupGuideUI.SURFACE).effect(RoundedNodeEffect.create(12F)).effect(BorderNodeEffect.create(PopupGuideUI.ELEMENT, 1.5F)).body(card -> { TextNode.create(400, 150).text(Text.create(file.get(), info)).anchor(Align.CENTER).attach(card); RectNode .create(300, 380, 200, 70) .color(PopupGuideUI.ACCENT) .label(Text.create("Delete", info.withColor(PopupGuideUI.ON_ACCENT))) .onClick(event -> JOID.open(new PopupGuideConfirmPopup(() -> file.set("No file")))) .attach(card);}).attach(this);Check: click Delete.
The popup opens over the dimmed screen, and the button of the screen ignores the mouse; Cancel or Escape closes the popup.
3. Run the action on confirm
Section titled “3. Run the action on confirm”The Delete button of the popup runs the action, then closes the popup with JOID.close:
RectNode.create(270, 190, 180, 70).color(PopupGuideConfirmPopup.ACCENT).label(Text.create("Delete", label)).onClick(event -> { this.action.run(); JOID.close(this);}).attach(card);Full file: PopupGuideConfirmPopup.java
Check: click Delete on the screen, then Delete in the popup: the file name changes to No file, and the popup closes.
4. Force an answer
Section titled “4. Force an answer”Add closeable = false to the @UIData of the popup, so that only its buttons close it (Configure a UI).
The color of the backdrop is backgroundColor of the same annotation, and the opening and closing animations are the transition of @UIDataPopup (Transitions).
Check: with closeable = false, Escape leaves the popup open.
If it does not work
Section titled “If it does not work”- The popup replaces the screen instead of opening over it: the class misses
@UIDataPopup(active = true); with@UIDataPopupalone, dev mode prints@UIDataPopup on … leaves active = false and changes nothing. - The screen behind is not dimmed: the class misses
@UIData(background = true). JOID.openthrowsIllegalStateException: No IUIBridge can open …: no backend is registered yet. Open the popup from a listener, or see Run code once JOID is ready.Escapedoes not close the popup:closeableisfalse, or a node of the popup, such as a focused text field, consumed the key (Know who receives the keyboard).- The popup disappears without its closing animation: it was closed with
JOID.forceClose, which skips the transition.
