Class ClipboardBinding
- All Implemented Interfaces:
Serializable
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 Summary
Modifier and TypeMethodDescriptionvoidwrite(ClipboardContent content) Copies a multi-format payload to the clipboard, packed into a singleClipboardItem.voidwrite(ClipboardContent content, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Likewrite(ClipboardContent)but reports the outcome back to the server.voidCopies a literal HTML string to the clipboard astext/htmlwhen the underlying trigger fires.voidwriteHtml(String literal, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteHtml(String)but reports the outcome back to the server.voidwriteImage(Component source) Copies the given component's root<img>element to the clipboard asimage/pngwhen the underlying trigger fires.voidwriteImage(Component source, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteImage(Component)but reports the outcome back to the server.voidwriteImage(DownloadHandler handler) Copies the image served by the givenDownloadHandlerto the clipboard asimage/pngwhen the underlying trigger fires.voidwriteImage(DownloadHandler handler, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteImage(DownloadHandler)but reports the outcome back to the server.writeText(C source) Copies the currentvalueproperty of the given component (typically an input field) to the clipboard astext/plainwhen the underlying trigger fires.writeText(C source, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteText(Component)but reports the outcome back to the server.voidCopies a literal string to the clipboard astext/plainwhen the underlying trigger fires.voidwriteText(String literal, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteText(String)but reports the outcome of the write promise back to the server.
-
Method Details
-
writeText
Copies a literal string to the clipboard astext/plainwhen the underlying trigger fires.- Parameters:
literal- the value to copy, notnull
-
writeText
public void writeText(String literal, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteText(String)but reports the outcome of the write promise back to the server.- Parameters:
literal- the value, notnullonCopied- UI-thread callback receiving the copied string, notnullonError- UI-thread callback receiving the browser's error, notnull
-
writeText
Copies the currentvalueproperty of the given component (typically an input field) to the clipboard astext/plainwhen 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 implementingHasValue<?, String>- Parameters:
source- the component whosevalueshould be copied, notnull
-
writeText
public <C extends Component & HasValue<?,String>> void writeText(C source, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteText(Component)but reports the outcome back to the server.- Type Parameters:
C- component type implementingHasValue<?, String>- Parameters:
source- the component whosevalueshould be copied, notnullonCopied- UI-thread callback receiving the copied string, notnullonError- UI-thread callback receiving the browser's error, notnull
-
writeHtml
Copies a literal HTML string to the clipboard astext/htmlwhen the underlying trigger fires.- Parameters:
literal- the HTML, notnull
-
writeHtml
public void writeHtml(String literal, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteHtml(String)but reports the outcome back to the server.- Parameters:
literal- the HTML, notnullonCopied- UI-thread callback receiving the copied HTML, notnullonError- UI-thread callback receiving the browser's error, notnull
-
writeImage
Copies the given component's root<img>element to the clipboard asimage/pngwhen the underlying trigger fires. The image is drawn on a canvas and exported as PNG on the client, so the source can be any rasterisable format (image/png,image/jpeg,image/svg+xml, ...) as long as it has intrinsic dimensions.Cross-origin images need
crossorigin="anonymous"on the<img>plus matching CORS headers; otherwise the canvas is tainted and the write fails. Same-origin anddata:URLs always work.- Parameters:
source- the component whose root<img>should be copied, notnull
-
writeImage
public void writeImage(Component source, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteImage(Component)but reports the outcome back to the server.onCopiedreceivesnull— the image-only write has no meaningful string value.- Parameters:
source- the component whose root<img>should be copied, notnullonCopied- UI-thread callback receivingnull, notnullonError- UI-thread callback receiving the browser's error, notnull
-
writeImage
Copies the image served by the givenDownloadHandlerto the clipboard asimage/pngwhen the underlying trigger fires.A hidden
<img>bound to the handler is appended to the trigger host, so the browser begins downloading the image as soon as this method is called — well before the user can click. At fire time the image is already decoded (or finishes decoding inside the canvas converter'sloadlistener), drawn onto a canvas, and exported as PNG.Cross-origin concerns do not apply because the handler is served by the same origin as the application.
- Parameters:
handler- the download handler producing the image bytes, notnull
-
writeImage
public void writeImage(DownloadHandler handler, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) LikewriteImage(DownloadHandler)but reports the outcome back to the server.onCopiedreceivesnull— the image-only write has no meaningful string value.- Parameters:
handler- the download handler producing the image bytes, notnullonCopied- UI-thread callback receivingnull, notnullonError- UI-thread callback receiving the browser's error, notnull
-
write
Copies a multi-format payload to the clipboard, packed into a singleClipboardItem.- Parameters:
content- the content, notnull; must have at least one slot set- Throws:
IllegalArgumentException- ifcontenthas no slots set
-
write
public void write(ClipboardContent content, SerializableConsumer<@Nullable String> onCopied, SerializableConsumer<PromiseAction.Error> onError) Likewrite(ClipboardContent)but reports the outcome back to the server.- Parameters:
content- the content, notnull; must have at least one slot setonCopied- UI-thread callback receiving thetext/plainvalue if present, otherwise thetext/htmlvalue, notnullonError- UI-thread callback receiving the browser's error, notnull
-