Repository navigation
Use the clipboard
fakeivchenko edited this page Sep 29, 2026
·
1 revision
The clipboard of the desktop is open to Java for text and images, and to the page for text,
without the permission prompts and the user gesture that navigator.clipboard asks for. In this
guide, the page copies and pastes text, and Java copies an image from the resources and pastes it
back into the page.

document.querySelector("#copy").addEventListener("submit", (event) => {
event.preventDefault();
lwjwae.clipboard.writeText(document.querySelector("#text").value);
});
document.querySelector("#paste").addEventListener("click", async () => {
const text = await lwjwae.clipboard.readText();
document.querySelector("#pasted").textContent = text === null ? "No text on the clipboard." : `Pasted: ${text}`;
});readText resolves to the text, or to null when the clipboard holds none, such as after an
image was copied.
public class ClipboardGuide {
public static void main(String[] args) {
Application.launch(
WindowParameters.of("Clipboard", 560, 460),
window -> {
Clipboard clipboard = window.application().clipboard();
window.bind("copyLogo", Void.class, _ -> ClipboardGuide.copyLogo(clipboard));
window.bind("pasteImage", Void.class, _ -> ClipboardGuide.pasteImage(clipboard));
window.loadResource("clipboard/index.html");
});
}
private static Void copyLogo(Clipboard clipboard) {
clipboard.writeImage("clipboard/logo.png");
return null;
}
/** The image on the clipboard as a data URL for an img, or null without one. */
private static String pasteImage(Clipboard clipboard) {
return clipboard
.readImage()
.join()
.map(png -> "data:image/png;base64," + Base64.getEncoder().encodeToString(png))
.orElse(null);
}
}| Method | What it does |
|---|---|
readText() |
The text, or empty when there is none |
writeText(String) |
Puts text on the clipboard |
readImage() |
The image as PNG bytes, whatever form the application that copied it chose, or empty |
writeImage(byte[]) |
Puts a PNG image on the clipboard, in the forms that other applications of the platform read |
writeImage(String) |
The same, from a PNG among the resources |
The reads return a CompletableFuture, because the application that owns the clipboard answers
when it can; a handler of a binding may join it, since it runs on a virtual thread of its own.
On the page, show the image that pasteImage returns:
document.querySelector("#copyLogo").addEventListener("click", () => window.copyLogo());
document.querySelector("#pasteImage").addEventListener("click", async () => {
const image = document.querySelector("#image");
const url = await window.pasteImage();
image.hidden = url === null;
if (url) image.src = url;
});-
copyLogoandpasteImagetakeVoid.class, which binds a function without an argument through the codec, so that it can return a value of any type; see Pass objects through the bridge. - Try Copy the logo, then paste into an image editor or a chat: the image is on the clipboard of the desktop, not just of the application.
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