6. Export notes through a native dialog - fakeivchenko/lwjwae GitHub Wiki

Dialogs of the platform are available from Java and from the page. In this step, an Export button asks where to save the notes and writes them to a text file.

  1. In Main.java, add the following imports:

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.util.Optional;
  2. At the end of setUp, bind a function named exportNotes:

    window.bind(
        "exportNotes",
        _ -> {
          Optional<Path> file = window.dialogs().pickSaveFile("notes.txt").join();
          if (file.isEmpty()) {
            return "Export canceled";
          }
          try {
            Files.write(file.get(), NOTES);
          } catch (IOException e) {
            return "Export failed: " + e.getMessage();
          }
          return "Exported to " + file.get();
        });

    window.dialogs() holds the dialogs of the window, and pickSaveFile shows the save dialog of the platform over it and returns a future. The handler runs on its own virtual thread, so join waits for the user without blocking the window. The future holds an empty Optional if the user cancels.

  3. In index.html, add the button after Save:

          <button id="save">Save</button>
          <button id="export">Export</button>
  4. In app.js, call the function:

    document.querySelector("#export").addEventListener("click", async () => {
      status.textContent = await window.exportNotes();
    });
  5. Run the application, save a note, and click Export. Pick a location in the dialog, and the status line shows where the file went.


Previous: Draw your own title bar · Next: Keep the application in the tray

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