Repository navigation
3. Call Java from the page
A binding makes a Java function callable from JavaScript. In this step, the Save button hands the note to Java, which stores it and answers with a status message.
-
In
Main.java, bind a function namedsaveNote:package com.example.notes; import dev.ivchenko.lwjwae.Application; import dev.ivchenko.lwjwae.Window; import dev.ivchenko.lwjwae.WindowParameters; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class Main { private static final List<String> NOTES = new CopyOnWriteArrayList<>(); public static void main(String[] args) { Application.launch(WindowParameters.of("Notes", "app/index.html"), Main::setUp); } static void setUp(Window window) { window.bind( "saveNote", note -> { NOTES.add(note); return "Saved " + NOTES.size() + (NOTES.size() == 1 ? " note" : " notes"); }); } }
setUpruns before the page loads, so the page findssaveNotefrom its first line of script. The handler runs on a virtual thread of its own, so it can block, read files, or call a server without freezing the window. -
In
src/main/resources/app/app.js, call the function when the user clicks Save:const note = document.querySelector("#note"); const status = document.querySelector("#status"); document.querySelector("#save").addEventListener("click", async () => { status.textContent = await window.saveNote(note.value); note.value = ""; });
Every binding is an
asyncfunction on the page: it returns a promise that resolves to what the Java handler returns. -
In
index.html, load the script at the end ofbody:<script src="app.js"></script> </body>
-
Run the application, write a note, and click Save:

Previous: Open a window · Next: Send events from Java to the page
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