Interface Clipboard
- All Superinterfaces:
Serializable
This interface offers two categories of clipboard operations:
- Client-side copy (reliable): Methods like
copyOnClick(Component, String)set up a click handler that executes the clipboard write directly in the browser event handler, satisfying the user gesture requirement in all browsers. - Server-initiated operations: Methods like
writeText(String)go through a server round-trip and may not work in Firefox or Safari due to user gesture timeout.
Usage pattern follows WebStorage — static methods that use
UI.getCurrentOrThrow() by default.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classCoordinates the asynchronous arrival of text/HTML metadata and uploaded file data for a paste event. -
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic RegistrationaddCopyListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a listener for copy events on the given component.static RegistrationaddCutListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a listener for cut events on the given component.static RegistrationaddPasteListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a listener for paste events on the given component.static ClipboardCopycopyImageOnClick(Component trigger, Component imageSource) Sets up a client-side click handler on the trigger component that copies the image from the given image component to the clipboard.static ClipboardCopycopyOnClick(Component trigger, Component source) Sets up a client-side click handler on the trigger component that copies the current value of the source component to the clipboard.static ClipboardCopycopyOnClick(Component trigger, String text) Sets up a client-side click handler on the trigger component that copies the given text to the clipboard when clicked.static ClipboardCopycopyOnClick(Component trigger, String text, Command onSuccess, Command onError) Sets up a client-side click handler on the trigger component that copies the given text to the clipboard, with success and error callbacks.static voidreadText(UI ui, SerializableConsumer<String> callback) Reads text from the clipboard using the specified UI.static voidreadText(SerializableConsumer<String> callback) Reads text from the clipboard.static PendingJavaScriptResultwriteImage(UI ui, StreamResource resource) Writes an image from the givenStreamResourceto the clipboard using the specified UI.static PendingJavaScriptResultwriteImage(UI ui, String imageUrl) Writes an image from the given URL to the clipboard using the specified UI.static PendingJavaScriptResultwriteImage(StreamResource resource) Writes an image from the givenStreamResourceto the clipboard.static PendingJavaScriptResultwriteImage(String imageUrl) Writes an image from the given URL to the clipboard.static PendingJavaScriptResultWrites the given text to the clipboard using the specified UI.static voidWrites the given text to the clipboard with success and error callbacks, using the specified UI.static PendingJavaScriptResultWrites the given text to the clipboard.static voidwriteText(String text, Command onSuccess, SerializableConsumer<String> onError) Writes the given text to the clipboard with success and error callbacks.
-
Method Details
-
copyOnClick
Sets up a client-side click handler on the trigger component that copies the given text to the clipboard when clicked.The copy operation executes entirely on the client side within the click event handler, so it satisfies the user gesture requirement in all browsers.
- Parameters:
trigger- the component whose clicks trigger the copytext- the text to copy to the clipboard- Returns:
- a
ClipboardCopyhandle for updating the text or removing the handler
-
copyOnClick
static ClipboardCopy copyOnClick(Component trigger, String text, Command onSuccess, Command onError) Sets up a client-side click handler on the trigger component that copies the given text to the clipboard, with success and error callbacks.The copy operation executes on the client side. The callbacks are invoked on the server after the clipboard operation completes or fails.
- Parameters:
trigger- the component whose clicks trigger the copytext- the text to copy to the clipboardonSuccess- callback invoked on the server when the copy succeedsonError- callback invoked on the server when the copy fails- Returns:
- a
ClipboardCopyhandle for updating the text or removing the handler
-
copyOnClick
Sets up a client-side click handler on the trigger component that copies the current value of the source component to the clipboard.The source component's value is read client-side (from the DOM element's
valueortextContentproperty) at click time, so no server round-trip is needed.- Parameters:
trigger- the component whose clicks trigger the copysource- the component whose value to copy- Returns:
- a
ClipboardCopyhandle for removing the handler
-
copyImageOnClick
Sets up a client-side click handler on the trigger component that copies the image from the given image component to the clipboard.The image source component should have a
srcattribute (e.g. an<img>element). The image is fetched client-side from the element'ssrcattribute and written to the clipboard as a blob.- Parameters:
trigger- the component whose clicks trigger the copyimageSource- the component with asrcattribute pointing to an image- Returns:
- a
ClipboardCopyhandle for removing the handler
-
writeText
Writes the given text to the clipboard.Browser compatibility note: This method involves a server round-trip and may not work in Firefox or Safari, which require clipboard operations to be performed within a user gesture (click handler call stack). Use
copyOnClick(Component, String)for reliable cross-browser clipboard writes.- Parameters:
text- the text to write to the clipboard- Returns:
- a
PendingJavaScriptResultfor the clipboard operation
-
writeText
Writes the given text to the clipboard using the specified UI.- Parameters:
ui- the UI to usetext- the text to write to the clipboard- Returns:
- a
PendingJavaScriptResultfor the clipboard operation
-
writeText
Writes the given text to the clipboard with success and error callbacks.Browser compatibility note: This method involves a server round-trip and may not work in Firefox or Safari.
- Parameters:
text- the text to writeonSuccess- callback invoked when the write succeedsonError- callback invoked with an error message when the write fails
-
writeText
Writes the given text to the clipboard with success and error callbacks, using the specified UI.- Parameters:
ui- the UI to usetext- the text to writeonSuccess- callback invoked when the write succeedsonError- callback invoked with an error message when the write fails
-
readText
Reads text from the clipboard.Browser compatibility note: This method involves a server round-trip. The browser may prompt the user for permission to read the clipboard.
- Parameters:
callback- callback invoked with the clipboard text
-
readText
Reads text from the clipboard using the specified UI.- Parameters:
ui- the UI to usecallback- callback invoked with the clipboard text
-
writeImage
Writes an image from the given URL to the clipboard.The browser fetches the image from the URL client-side and writes it to the clipboard as a PNG blob.
Browser compatibility note: This method involves a server round-trip for delivering the JavaScript command.
- Parameters:
imageUrl- the URL of the image to copy- Returns:
- a
PendingJavaScriptResultfor the clipboard operation
-
writeImage
Writes an image from the given URL to the clipboard using the specified UI.- Parameters:
ui- the UI to useimageUrl- the URL of the image to copy- Returns:
- a
PendingJavaScriptResultfor the clipboard operation
-
writeImage
Writes an image from the givenStreamResourceto the clipboard.The stream resource is registered in the session and the browser fetches it client-side.
- Parameters:
resource- the stream resource providing the image data- Returns:
- a
PendingJavaScriptResultfor the clipboard operation
-
writeImage
Writes an image from the givenStreamResourceto the clipboard using the specified UI.- Parameters:
ui- the UI to useresource- the stream resource providing the image data- Returns:
- a
PendingJavaScriptResultfor the clipboard operation
-
addPasteListener
static Registration addPasteListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a listener for paste events on the given component.The listener receives text, HTML, and file data from the paste event. Pasted files are transferred to the server via the upload mechanism.
- Parameters:
target- the component to listen for paste events onlistener- the paste event listener- Returns:
- a registration for removing the listener
-
addCopyListener
static Registration addCopyListener(Component target, SerializableConsumer<ClipboardEvent> listener) Adds a listener for copy events on the given component.- Parameters:
target- the component to listen for copy events onlistener- the copy event listener- Returns:
- a registration for removing the listener
-
addCutListener
Adds a listener for cut events on the given component.- Parameters:
target- the component to listen for cut events onlistener- the cut event listener- Returns:
- a registration for removing the listener
-