Skip to content

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.

Text and an image through the clipboard, on Linux and on Windows 11

Text on 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.

Text and images in Java

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;
});

Good to know

  • copyLogo and pasteImage take Void.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.

Clone this wiki locally