4. Send events from Java to the page - fakeivchenko/lwjwae GitHub Wiki

Java can also start the conversation. An event carries a name and a payload from Java to every listener on the page. In this step, Java announces each new note, and the page adds it to a list.

  1. In Main.java, emit an event after the note is stored:

    static void setUp(Window window) {
      window.bind(
          "saveNote",
          note -> {
            NOTES.add(note);
            window.emit("noteAdded", note);
            return "Saved " + NOTES.size() + (NOTES.size() == 1 ? " note" : " notes");
          });
    }
  2. In index.html, add a list after the status line:

          <p id="status"></p>
          <ul id="notes"></ul>
  3. In app.js, listen for the event:

    lwjwae.listen("noteAdded", (event) => {
      const item = document.createElement("li");
      item.textContent = event.payload;
      document.querySelector("#notes").append(item);
    });

    window.lwjwae is the page side of the library. Besides listen, it has emit to send events to Java, window to control the window, dialog for native dialogs, and clipboard.

  4. Run the application and save a few notes:

    Each saved note appears in the list


Previous: Call Java from the page · Next: Draw your own title bar

⚠️ **GitHub.com Fallback** ⚠️