Can anyone help me to copy the text to clipboard in a simple way and which works on all browsers

1,106 views
Skip to first unread message

Dhinakar Reddy Pothireddi

unread,
Mar 21, 2016, 9:46:23 AM3/21/16
to GWT Users
Can anyone help me to copy the text to clipboard in a simple way and which works on all browsers especially on Safari, Chrome and IE and with out the need of flash or any other apps. Thanks in advance

        public static native void copyFrom(com.google.gwt.user.client.Element element) /*-{
  $wnd.window.clipboardData.setData('text', element.innerText);
}-*/;
public static native void pasteTo(com.google.gwt.user.client.Element element) /*-{
  $wnd.window.clipboardData.getData(element);
}-*/;
This code only works on IE and returns an exception on Chrome.

I need to process this event on a button click.

Ed

unread,
Mar 21, 2016, 10:18:50 AM3/21/16
to Google Web Toolkit
Hi,

Try this:
                                 
                             final HTML CopyIcon = new HTML("Copy");
                                    CopyIcon.setStyleName("gwt-HTMLGreen");
                                    CopyIcon.setTitle("Drag and Drop");
                                    CopyIcon.getElement().setDraggable(Element.DRAGGABLE_TRUE);

                                    CopyIcon.addDomHandler(new DragStartHandler() {

                                        @Override
                                        public void onDragStart(DragStartEvent event) {

                                            event.setData("text/plain", YOURDATATOCOPY);

                                            event.getDataTransfer().setDragImage(CopyIcon.getElement(), 10, 10);

                                        }

                                    }, DragStartEvent.getType());

Hope this helps

Ed


--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

Dhinakar Reddy Pothireddi

unread,
Mar 28, 2016, 3:06:42 PM3/28/16
to GWT Users
Sorry Ed, I didn't work and even this is not what I need. I want to access the clipboard in the Chrome with some appropriate JSNI. The code I posted previously works for IE but fails on Chrome. Thanks.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsub...@googlegroups.com.

JonL

unread,
Mar 29, 2016, 7:50:01 PM3/29/16
to GWT Users

Vassilis Virvilis

unread,
Mar 30, 2016, 6:00:21 AM3/30/16
to google-we...@googlegroups.com
That was indeed very insightful and helpful. Thanks for sharing and congrats to the lucid guys for the guide.

To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.

To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.

To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.



--
Vassilis Virvilis

Harald Pehl

unread,
Mar 30, 2016, 7:27:50 AM3/30/16
to GWT Users
I'm using an implementation based on http://zeroclipboard.org/ [1]. ZeroClipboard is used by GitHub and should work on all modern browsers. My implementation is built using JsInterop and provides a subset of ZeroClipboard's API. It should be pretty easy to add more methods if you need them. 

Here's a sample how to use it:

Element button = Browser.getDocument().createButtonElement();
Clipboard clipboard = new Clipboard(button);
clipboard
.onCopy(event -> event.client.setText("This text gets copied to the clipboard"));
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsub...@googlegroups.com.

To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsub...@googlegroups.com.

To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.



--
Vassilis Virvilis
Reply all
Reply to author
Forward
Message has been deleted
0 new messages