Skip to content

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.

The popup over the screen that opened it. The screen behind is dimmed and receives no input.

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.

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);

Full file: PopupGuideUI.java

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.

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.

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.

  1. The popup replaces the screen instead of opening over it: the class misses @UIDataPopup(active = true); with @UIDataPopup alone, dev mode prints @UIDataPopup on … leaves active = false and changes nothing.
  2. The screen behind is not dimmed: the class misses @UIData(background = true).
  3. JOID.open throws IllegalStateException: No IUIBridge can open …: no backend is registered yet. Open the popup from a listener, or see Run code once JOID is ready.
  4. Escape does not close the popup: closeable is false, or a node of the popup, such as a focused text field, consumed the key (Know who receives the keyboard).
  5. The popup disappears without its closing animation: it was closed with JOID.forceClose, which skips the transition.

Report a problem with this page