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

时间:2009-08-22 21:07:14

标签: gwt clipboard

使用Google搜索找不到任何相关信息。

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

任何帮助或指示赞赏。

5 个答案:

答案 0 :(得分:5)

我使用了ZeroClipboard和GWT(正如亚历山大所建议的那样),但这并不简单。

请参阅http://blog.dandoy.org/2011/09/using-zeroclipboard-with-gwt.html

答案 1 :(得分:3)

目前似乎没有任何GWT库提供此功能。无论如何,由于需要Flash,因此无法在所有浏览器中支持此功能。一个比包装功能更好的库是ZeroClipboard

答案 2 :(得分:2)

GWT本身不支持//your original javascript function that returns json function getJsonData() { // your code } // use that js function to store in a var , then pass it to your c# var MyJSONdata = getJSONData(); $.ajax({ type: 'POST', url: 'YourPage.aspx/c#MethodThatWillReceiveJSON', data: "{c#MethodParameterName:" + JSON.stringify(MyJSONdata) + "}", contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (r) { alert(r.d.exampleDataFromJson); } }); 命令,但它非常简单。

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

$doc.execCommand('copy');

答案 3 :(得分:2)

只需打开提供的答案https://stackoverflow.com/a/30810322/106261

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

不需要任何带有新浏览器的库。

所以:

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);
    }-*/;

答案 4 :(得分:1)

这里有一个没有原生JS的解决方案,但是 gwt元素,仍然受到@SushmithaShenoy的启发,留待这里以供将来参考。

前提:

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

Label.getElement().setAttribute("id","your_element_id"); //unique ID!

现在是“真正的”代码,可能放在点击处理程序中:

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();