Skip to content

Open several windows

fakeivchenko edited this page Oct 9, 2026 · 2 revisions

An application can show as many windows as it needs, opened from Java or from a page, and they can talk to each other through Java. In this guide, a board collects the messages of note windows: Java opens the board and the first note, the board opens more notes, and every window hears every message.

A board and two notes, on Linux and on Windows 11

Open windows from Java

Application.launch is the short form for one window. For more, create the application, show the windows, and run it:

public class Windows {
  public static void main(String[] args) {
    try (Application application = Application.create()) {
      // A binding of the application reaches every window, and learns which one called.
      application.bind(
          "post",
          String.class,
          (Window window, String text) -> {
            application.emit("posted", new Post(window.title(), text));
            return null;
          });
      application.show(
          WindowParameters.builder()
              .title("Board")
              .size(new WindowSize(460, 420))
              .position(new WindowPosition(60, 60))
              .resource("windows/board.html")
              .build());
      application.show(
          WindowParameters.builder()
              .title("Note 1")
              .size(new WindowSize(360, 240))
              .position(new WindowPosition(560, 60))
              .resource("windows/note.html")
              .build());
      application.run();
    }
  }
}

with a record for the message:

public record Post(String from, String text) {}
  • show opens a window and shows it; open only opens it, for a window that should appear later with window.show().
  • run returns when the last window closes, and quit closes them all.
  • A binding, a handler, or a listener on the Application belongs to every window, the ones that open later too. Its handler can take the Window that called as its first argument.
  • application.emit reaches the page of every window; window.emit reaches one.

Open windows from a page

The board opens a note with lwjwae.open, whose options are those of WindowParameters, and hears the messages:

let notes = 1;

document.querySelector("#open").addEventListener("click", () => {
  notes++;
  lwjwae.open({ title: `Note ${notes}`, width: 360, height: 240, x: 560, y: 60 + (notes - 1) * 290,
    resource: "windows/note.html" });
});

lwjwae.listen("posted", ({ payload }) => {
  const item = document.createElement("li");
  item.innerHTML = `<b></b><span></span>`;
  item.querySelector("b").textContent = payload.from;
  item.querySelector("span").textContent = payload.text;
  document.querySelector("#posts").append(item);
});

A note posts through the binding of the application, and closes itself:

document.querySelector("#note").addEventListener("submit", (event) => {
  event.preventDefault();
  window.post(document.querySelector("#text").value);
  document.querySelector("#text").value = "";
});
document.querySelector("#close").addEventListener("click", () => lwjwae.close());

lwjwae.open resolves to the id of the new window, which application.window(id) finds in Java.

Open child and modal windows

A window can belong to another one. A child window, such as a palette, stays above its parent, opens centered over it unless it has a position, and closes with it. A modal window, such as a preferences dialog, also keeps the user from its parent until it closes, while the other windows of the application stay usable.

An editor with its palette and a modal Preferences window, on Linux and on Windows 11

Java opens the palette as a child of the editor with parent:

public class ChildWindows {
  public static void main(String[] args) {
    try (Application application = Application.create()) {
      Window editor =
          application.show(
              WindowParameters.builder()
                  .title("Editor")
                  .size(new WindowSize(640, 440))
                  .position(new WindowPosition(80, 80))
                  .resource("child/editor.html")
                  .build());
      application.show(
          WindowParameters.builder()
              .title("Palette")
              .size(new WindowSize(220, 300))
              .position(new WindowPosition(740, 80))
              .parent(editor)
              .resource("child/palette.html")
              .build());
      application.run();
    }
  }
}

The editor opens Preferences from its page with modal: true, which makes the window that calls the parent. child: true opens a child that isn't modal:

document.querySelector("#preferences").addEventListener("click", () =>
  lwjwae.open({ title: "Preferences", width: 380, height: 260, resource: "child/preferences.html", modal: true }));

In Java, .parent(window).modal(true) does the same, and window.parent() tells which window a window belongs to. A modal window needs a parent.

Platform A child window A modal window
Windows A window owned by its parent. The owner is turned off with EnableWindow while the window is open.
macOS A child window of AppKit, which moves with its parent. A sheet of its parent.
Linux, GTK 3 and GTK 4 A transient window of its parent. A window group of its own, so it blocks only its parent.

Good to know

  • Post goes through the codec, so the application needs one; see Pass objects through the bridge.
  • application.windows() lists the open windows. A window that closes leaves the list, and its listeners and bindings go with it.
  • On Linux with GTK 4, and on Wayland, a window can't choose its place: the position is a wish that the desktop may ignore. See Place and control a window.

Clone this wiki locally