Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用GWT复制到剪贴板?_Gwt_Clipboard - Fatal编程技术网

如何使用GWT复制到剪贴板?

如何使用GWT复制到剪贴板?,gwt,clipboard,Gwt,Clipboard,在谷歌搜索中找不到任何相关信息 有人知道如何通过GWT Java代码将一些文本复制到剪贴板吗? 我希望避免使用原始javascript注入解决方案 感谢您的帮助或指点。目前似乎没有任何GWT库提供此功能。无论如何,不可能在所有浏览器中都支持这一点,因为需要Flash。这是一个相当不错的库,而不是包装它的功能。我使用过GWT(正如Alexander所建议的),但它并不简单 请参见以下代码在chrome中运行良好: public static native void copyToClipboard(

在谷歌搜索中找不到任何相关信息

有人知道如何通过GWT Java代码将一些文本复制到剪贴板吗? 我希望避免使用原始javascript注入解决方案


感谢您的帮助或指点。

目前似乎没有任何GWT库提供此功能。无论如何,不可能在所有浏览器中都支持这一点,因为需要Flash。这是一个相当不错的库,而不是包装它的功能。

我使用过GWT(正如Alexander所建议的),但它并不简单


请参见

以下代码在chrome中运行良好:

public static native void copyToClipboard() /*-{
    var selection = $wnd.getSelection();
    var text =  $doc.getElementById("myElement");
    var range = $doc.createRange();
    range.selectNodeContents(text);
    selection.removeAllRanges();
    selection.addRange(range);
    $doc.execCommand('copy');
    selection.removeAllRanges();
}-*/;

这里是一个没有原生JS的解决方案,但是gwt elemental,仍然受到@SushmithaShenoy的启发,这里留作将来参考

前提条件:

import elemental.client.Browser;
import elemental.html.Selection;
import elemental.ranges.Range;

Label.getElement().setAttribute("id","your_element_id"); //unique ID!
现在,“真实”代码可能放在clickhandler中:

final Selection selection = Browser.getWindow().getSelection();
final Range range = Browser.getDocument().createRange();
range.selectNodeContents(Browser.getDocument().getElementById(""you_elements_id"));
selection.removeAllRanges();
selection.addRange(range);
Browser.getWindow().getDocument().execCommand("copy", false, "");
selection.removeAllRanges();

GWT本机不支持
$doc.execCommand('copy')命令,但非常简单

首先在项目上设置焦点,选择文本,然后复制它

myTextBox.setFocus(true);
myTextBox.selectAll();
boolean success = copyToClipboard();

private static native boolean copyToClipboard() /*-{
    return $doc.execCommand('copy');
}-*/;

只需将提供的答案包装起来

因此,将任何文本传递给javascript本机函数/方法,js函数将创建一个新元素并复制到剪贴板,并在复制后删除该元素

新浏览器不需要任何lib

因此:


gwt实际上将您的java代码编译成javascript,所以当它运行时,它实际上是在运行javascript。我知道人,但如果我不编写它,我就不必担心它在所有浏览器上工作。这意味着gwt代码中的原始javascript注入我知道这篇文章很古老,但仍然很难在网上找到合适的例子。塞德里克下面的答案是我找到的最接近于解决这个问题的答案。如果有人有一个好的链接,我很乐意接受Cheers@Jonathan不幸的是,ZeroClipboard的官方网站已经过时,源代码存储库也已经过时,请参见下面的内容。你是这方面的传奇人物。大脑筋运动。谢谢:)
public static native void copyTextToClipboard(String text) /*-{
        var textArea = document.createElement("textarea");
        //
        // *** This styling is an extra step which is likely not required. ***
        //
        // Why is it here? To ensure:
        // 1. the element is able to have focus and selection.
        // 2. if element was to flash render it has minimal visual impact.
        // 3. less flakyness with selection and copying which **might** occur if
        //    the textarea element is not visible.
        //
        // The likelihood is the element won't even render, not even a flash,
        // so some of these are just precautions. However in IE the element
        // is visible whilst the popup box asking the user for permission for
        // the web page to copy to the clipboard.
        //

        // Place in top-left corner of screen regardless of scroll position.
        textArea.style.position = 'fixed';
        textArea.style.top = 0;
        textArea.style.left = 0;

        // Ensure it has a small width and height. Setting to 1px / 1em
        // doesn't work as this gives a negative w/h on some browsers.
        textArea.style.width = '2em';
        textArea.style.height = '2em';

        // We don't need padding, reducing the size if it does flash render.
        textArea.style.padding = 0;

        // Clean up any borders.
        textArea.style.border = 'none';
        textArea.style.outline = 'none';
        textArea.style.boxShadow = 'none';

        // Avoid flash of white box if rendered for any reason.
        textArea.style.background = 'transparent';


        textArea.value = text;

        document.body.appendChild(textArea);

        textArea.select();

        try {
            var successful = document.execCommand('copy');
        } catch (err) {
            console.log('Unable to copy');
        }
        document.body.removeChild(textArea);
    }-*/;