ClipboardTrigger

ClipboardTrigger is a facet that allows a user to copy text from a field to the clipboard.

Component’s XML-name: clipboardTrigger.

Basics

ClipboardTrigger is defined in the facets element of the screen XML descriptor and has the following attributes:

1. input - defines id of the component from which the text content will be copied. It is possible to associate the ClipboardTrigger with the following components:

2. button - defines id of the Button that triggers the copying.

With the help of isSupportedByWebBrowser() method you can check whether the user’s browser supports copying or not.

Usage example:

<window xmlns="http://jmix.io/schema/ui/window"
        caption="msg://clipboardTriggerScreen.caption">
    <facets>
        <clipboardTrigger id="clipboardTrigger"
                          button="btn"
                          input="maskedField"/>
    </facets>
    <layout>
        <hbox spacing="true">
            <maskedField id="maskedField"
                         caption="Phone number"
                         mask="(###) ###-##-##"/>
            <button id="btn"
                    caption="Copy"
                    align="BOTTOM_RIGHT"/>
        </hbox>
    </layout>
</window>
@Autowired
private Notifications notifications;
@Autowired
private ClipboardTrigger clipboardTrigger;

@Subscribe("btn")
public void onBtnClick(Button.ClickEvent event) {
    if (clipboardTrigger.isSupportedByWebBrowser()) {
        notifications.create()
                .withCaption("Phone number was copied to clipboard")
                .show();
    } else {
        notifications.create()
                .withCaption("The phone number is not copied, because your " +
                        "browser does not support this functionality")
                .show();
    }
}

Events and Handlers

To generate a handler stub in Jmix Studio, select the facet in the screen descriptor XML or in the Jmix UI hierarchy panel and use the Handlers tab of the Jmix UI inspector panel.

Alternatively, you can use the Generate Handler button in the top panel of the screen controller.

CopyEvent

This event fires when the user clicks on the button related to ClipboardTrigger and has the following methods:

  • isSuccess() - returns true if the text content was successfully copied to the client-side clipboard.

  • getSource() - returns the object of ClipboardTrigger on which the event initially occurred.

@Subscribe("clipboardTrigger")
public void onClipboardTriggerCopy(ClipboardTrigger.CopyEvent event) {
    if (event.isSuccess()) {
        notifications.create()
                .withCaption("Text was successfully copied to clipboard")
                .show();
    } else {
        notifications.create()
                .withCaption("Something went wrong during copying")
                .show();
    }
}

Programmatic registration of the event handler: use the addCopyListener() facet method.

All XML Attributes

You can view and edit attributes applicable to the facet using the Jmix UI inspector panel of the Studio’s Screen Designer.