Repository navigation
6. Export notes through a native dialog
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.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(); });
pickSaveFileshows the save dialog of the platform over the window 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
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