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.
-
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;
-
At the end of
setUp, bind a function namedexportNotes: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, andpickSaveFileshows the save dialog of the platform over it and returns a future. The handler runs on its own virtual thread, sojoinwaits for the user without blocking the window. The future holds an emptyOptionalif the user cancels. -
In
index.html, add the button after Save:<button id="save">Save</button> <button id="export">Export</button>
-
In
app.js, call the function:document.querySelector("#export").addEventListener("click", async () => { status.textContent = await window.exportNotes(); });
-
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