Class ClipboardApi

java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.clipboard.ClipboardApi

public class ClipboardApi extends JavaScriptApi
Provides access to the browser's Clipboard API from server-side Java code.

This class wraps the browser's navigator.clipboard API, enabling server-side code to read from and write to the system clipboard. Reading from the clipboard is asynchronous and dispatches events to the requesting element.

Methods:

Usage:


 public class CopyPage extends HtmlPage implements ClipboardListener {

     private TextInput input;
     private Div output;

     @Override
     protected void createBody(Body body) {
         input = new TextInput();
         body.addElement(input);

         Button copyBtn = new Button("Copy");
         copyBtn.registerListener((MouseClickedEvent e) -> {
             ClipboardApi.writeText(input.getValue());
         }, MouseClickedEvent.class);
         body.addElement(copyBtn);

         Button pasteBtn = new Button("Paste");
         pasteBtn.registerListener(this, ClipboardReadEvent.class);
         pasteBtn.registerListener(this, ClipboardErrorEvent.class);
         pasteBtn.registerListener((MouseClickedEvent e) -> {
             ClipboardApi.readText(pasteBtn);
         }, MouseClickedEvent.class);
         body.addElement(pasteBtn);

         output = new Div();
         body.addElement(output);
     }

     @Override
     public void onEvent(ClipboardReadEvent event) {
         output.setText("Pasted: " + event.getText());
     }

     @Override
     public void onEvent(ClipboardErrorEvent event) {
         output.setText("Error: " + event.getMessage());
     }
 }
 

Browser Support:

The Clipboard API requires a secure context (HTTPS) and user permission. Browsers will prompt the user for permission before allowing clipboard access.

Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • ClipboardApi

      public ClipboardApi()
  • Method Details

    • writeText

      public static void writeText(String text)
      Writes text to the clipboard using the current page context.

      This operation is synchronous from the server's perspective - the text is sent to the client and written to the clipboard. No event is dispatched on success.

      Parameters:
      text - The text to write to the clipboard.
    • writeText

      public static void writeText(HtmlPage page, String text)
      Writes text to the clipboard.
      Parameters:
      page - The page context.
      text - The text to write to the clipboard.
    • readText

      public static void readText(Element element)
      Reads text from the clipboard using the current page context.

      This is an asynchronous operation. When the text is read, a ClipboardReadEvent is dispatched to the element. If an error occurs (e.g., permission denied), a ClipboardErrorEvent is dispatched instead.

      Parameters:
      element - The element that will receive the clipboard read event.
    • readText

      public static void readText(HtmlPage page, Element element)
      Reads text from the clipboard.
      Parameters:
      page - The page context.
      element - The element that will receive the clipboard read event.