Class Clipboard
- All Implemented Interfaces:
Serializable
onClick(Component):
Button copyButton = new Button("Copy");
Clipboard.onClick(copyButton).writeText(textField);
Clipboard.onClick(copyButton)
.write(ClipboardContent.create().text("Hello").html("<b>Hello</b>"));
The Clipboard API requires a fresh user gesture for each write, so actions
only run during the DOM event that fires the underlying trigger.
Read-side support is exposed through
onPaste, which forwards the
browser's native paste event to a server-side listener as a
PasteEvent. Unlike the write API, onPaste does not need a
click binding — it attaches a DOM listener directly to the given
component and fires on every paste gesture targeting it (or any of its
descendants, since paste bubbles). Pass the
UI as the component for UI-wide scope;
the options
overload lets the application skip pastes whose target is a form field
— useful for page-wide listeners that should only react to pastes
intended for the page as a whole.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Component & ClickNotifier<?>>
ClipboardBindingonClick(T component) Registers the given component as a clickable trigger for a clipboard action — the common shape for copy-to-clipboard buttons.static RegistrationonPaste(Component component, PasteOptions options, SerializableConsumer<PasteEvent> listener) Registers a listener for browserpasteevents on the given component with the givenPasteOptions.static RegistrationonPaste(Component component, SerializableConsumer<PasteEvent> listener) Registers a listener for browserpasteevents on the given component.
-
Method Details
-
onClick
Registers the given component as a clickable trigger for a clipboard action — the common shape for copy-to-clipboard buttons. Equivalent tonew ClickTrigger(component), without making callers reach for the trigger framework's internal types.- Type Parameters:
T- the component type, must implementClickNotifier- Parameters:
component- the component to listen for clicks on, notnull- Returns:
- a new binding that can chain actions to this trigger
-
onPaste
Registers a listener for browserpasteevents on the given component. The listener is invoked on the UI thread once per paste gesture targetingcomponent(or any descendant, sincepastebubbles) with aPasteEventcarrying thetext/plainandtext/htmlrepresentations of the pasted content.The browser only fires
pastewhen the target element is focused at the moment the user invokes paste. For non-editable elements such as aDivthis means the element must be made focusable, typically viatabindex="0". SeePasteEventfor the rest of the browser caveats.Example:
Div pasteTarget = new Div(); pasteTarget.getElement().setAttribute("tabindex", "0"); add(pasteTarget); Clipboard.onPaste(pasteTarget, event -> { if (event.hasHtml()) { renderHtml(event.getHtml()); } else if (event.hasText()) { renderText(event.getText()); } });- Parameters:
component- the component to listen for paste events on, notnulllistener- the listener invoked for each paste, notnull- Returns:
- a
Registrationwhoseremovedetaches the paste listener
-
onPaste
public static Registration onPaste(Component component, PasteOptions options, SerializableConsumer<PasteEvent> listener) Registers a listener for browserpasteevents on the given component with the givenPasteOptions. The listener is invoked on the UI thread for each paste gesture targetingcomponent(or any descendant, sincepastebubbles) whose target matches the options. For UI-wide scope, pass theUIas the component; the UI's root element is<body>so it receives every paste event that bubbles up from anywhere on the page.The component does not need to be attached at registration time — the underlying DOM listener is bound to the component's element and is applied when the element is attached to a UI.
Pass
PasteOptions.defaults()to skip pastes whose target is an input, textarea, orcontenteditableelement (typically what a page-wide listener wants). PassPasteOptions.includingInputFields()to observe every paste regardless of focus.- Parameters:
component- the component to listen for paste events on, notnulloptions- paste filtering options, notnulllistener- the listener invoked for each matching paste, notnull- Returns:
- a
Registrationwhoseremovedetaches the paste listener
-