Copy to clipboard by clicking button

Hi,

I am adding a button to a browser screen, I want to implement that when the button clicked, an attribute (String type) of the selected entity is copied to the local clipboard.

Is there a way to implement that?

Best Regards.
Gabriel

1 Like

Hi,

I would suggest using the JsClipboard Vaadin add-on version 1.0.3 (newer versions use Vaadin 8).

Just add the dependency to the Web Module in the build.gradle file:

configure(webModule) {
    ...
    dependencies {
        ...
        compile('com.github.vaadin4qbanos:jsclipboard:1.0.3')

    }
...

And add something like this to add entity attribute value to a clipboard:

public class ProductEdit extends AbstractEditor<Product> {
    @Inject
    private Button copyButton;
    @Inject
    private Datasource<Product> productDs;

    @Override
    protected void postInit() {
        final JSClipboard clipboard = new JSClipboard();
        clipboard.setText(getItem().getName());
        clipboard.apply(copyButton.unwrap(com.vaadin.ui.Button.class));

        productDs.addItemPropertyChangeListener(e -> {
            if ("name".equals(e.getProperty())) {
                showNotification("Text to Copy: " + getItem().getName());
                clipboard.setText(getItem().getName()); 
            }
        });
    }
}
5 Likes

It works, Thank you very much

Gleb, can you tell me is it possible to use JsClipboard with ListComponent actions without any buttons? I want to prepare and copy http-link to the selected entity.