Class ClipboardBinding

java.lang.Object
com.vaadin.flow.component.clipboard.ClipboardBinding
All Implemented Interfaces:
Serializable

public final class ClipboardBinding extends Object implements Serializable
Fluent surface returned from Clipboard.onClick(T). Each write* action attaches one WriteToClipboardAction to the underlying Trigger.

Actions come in two flavours: fire-and-forget (one argument) and observed (with onCopied/onError callbacks). onCopied receives the string that was copied; onError receives the browser's error. Both consumers are required in the observed form — pass s -> {} or err -> {} to opt out of one.


 Button copy = new Button("Copy");
 Clipboard.onClick(copy).writeText(textField);

 Clipboard.onClick(copy).writeText(textField,
         copied -> Notification.show("Copied " + copied),
         err -> Notification.show("Failed: " + err.message()));
 
See Also:
  • Method Details

    • writeText

      public void writeText(String literal)
      Copies a literal string to the clipboard as text/plain when the underlying trigger fires.
      Parameters:
      literal - the value to copy, not null
    • writeText

      public void writeText(String literal, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError)
      Like writeText(String) but reports the outcome of the write promise back to the server.
      Parameters:
      literal - the value, not null
      onCopied - UI-thread callback receiving the copied string, not null
      onError - UI-thread callback receiving the browser's error, not null
    • writeText

      public <C extends Component & HasValue<?, String>> void writeText(C source)
      Copies the current value property of the given component (typically an input field) to the clipboard as text/plain when the underlying trigger fires. The value is read on the client at the moment the trigger fires, so subsequent edits are reflected without any server round-trip.
      Type Parameters:
      C - component type implementing HasValue<?, String>
      Parameters:
      source - the component whose value should be copied, not null
    • writeText

      public <C extends Component & HasValue<?, String>> void writeText(C source, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError)
      Like writeText(Component) but reports the outcome back to the server.
      Type Parameters:
      C - component type implementing HasValue<?, String>
      Parameters:
      source - the component whose value should be copied, not null
      onCopied - UI-thread callback receiving the copied string, not null
      onError - UI-thread callback receiving the browser's error, not null
    • writeHtml

      public void writeHtml(String literal)
      Copies a literal HTML string to the clipboard as text/html when the underlying trigger fires.
      Parameters:
      literal - the HTML, not null
    • writeHtml

      public void writeHtml(String literal, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError)
      Like writeHtml(String) but reports the outcome back to the server.
      Parameters:
      literal - the HTML, not null
      onCopied - UI-thread callback receiving the copied HTML, not null
      onError - UI-thread callback receiving the browser's error, not null
    • write

      public void write(ClipboardContent content)
      Copies a multi-format payload to the clipboard, packed into a single ClipboardItem.
      Parameters:
      content - the content, not null; must have at least one slot set
      Throws:
      IllegalArgumentException - if content has no slots set
    • write

      public void write(ClipboardContent content, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError)
      Like write(ClipboardContent) but reports the outcome back to the server.
      Parameters:
      content - the content, not null; must have at least one slot set
      onCopied - UI-thread callback receiving the text/plain value if present, otherwise the text/html value, not null
      onError - UI-thread callback receiving the browser's error, not null