Currently, my implementation works in IE but not in Chrome.
I have a button labelled "Paste Text" which when clicked, should paste the contents currently stored in the clipboard to a text box. Both the button and text box are created in the same Java class that the following functions are defined.
I have a function that returns a ClickHandler doing the paste action. It calls a native function getClipboardContents() to read the current clipboard data:
ClickHandler pasteClipboardToTextBox(final CustomTextBox textBox) {
return new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
textBox.setValue(getClipboardContents());
}
};
}
The following function uses JS to get the clipboard data:
private native String getClipboardContents() /*-{
return window.clipboardData.getData('Text');
}-*/;
I can add the ClickHandler to the paste button so when it's clicked, it'll paste the contents of clipboard into the passed text box object:
pasteButton.addClickHandler(pasteClipboardToTextBox(myTextBox));
I'm aware that this line:
window.clipboardData.getData('Text')
does not work in Chrome, so I'd need an alternative method of getting the clipboard data in a similar way, but that works in Chrome. I've heard that its possible to use
e.clipboardData.getData('text/plain')
but I'm unsure how to get this working in a native Java function.
Any help would be much appreciated!