Repository navigation
Open several windows
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.

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) {}-
showopens a window and shows it;openonly opens it, for a window that should appear later withwindow.show(). -
runreturns when the last window closes, andquitcloses them all. - A binding, a handler, or a listener on the
Applicationbelongs to every window, the ones that open later too. Its handler can take theWindowthat called as its first argument. -
application.emitreaches the page of every window;window.emitreaches one.
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.
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.

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. |
-
Postgoes 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.
Tutorial
- Set up the project
- Open a window
- Call Java from the page
- Send events from Java to the page
- Draw your own title bar
- Export notes through a native dialog
- Keep the application in the tray
- Show notifications
- Give the application an icon and a name
- Build a native executable
- Package the application
Guides
- Pass objects through the bridge
- Stream bytes between the page and Java
- Open several windows
- Place and control a window
- Make a transparent window
- Add menus
- Register global shortcuts
- Show progress in the taskbar
- Use the clipboard
- Handle dropped files
- Zoom the page
- Set the window icon
- Follow the theme of the desktop
- Use cookies
- Store data in SQLite
- Keep secrets
- Show dialogs
- Run a single instance
- Open your own links and files
- Update the application
- Handle page permissions
- Handle links and page loads
Reference