使用Java将文本复制到剪贴板

时间:2011-07-15 16:41:03

标签: java swing text clipboard

我想将文本从JTable的单元格复制到剪贴板,使其可以粘贴到其他程序(如Microsoft Word)中。我有来自JTable的文字,但我不确定如何将其复制到剪贴板。

7 个答案:

答案 0 :(得分:230)

这适合我,非常简单:

导入这些:

import java.awt.datatransfer.StringSelection;
import java.awt.Toolkit;

然后将这段代码放在您想要更改剪贴板的地方:

String myString = "This text will be copied into clipboard";
StringSelection stringSelection = new StringSelection(myString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);

答案 1 :(得分:12)

以下类允许您将字符串复制/粘贴到剪贴板中。

import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class SystemClipboard
{
    public static void copy(String text)
    {
        Clipboard clipboard = getSystemClipboard();

        clipboard.setContents(new StringSelection(text), null);
    }

    public static void paste() throws AWTException
    {
        Robot robot = new Robot();

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_V);
    }

    public static String get() throws Exception
    {
        Clipboard systemClipboard = getSystemClipboard();
        DataFlavor dataFlavor = DataFlavor.stringFlavor;

        if (systemClipboard.isDataFlavorAvailable(dataFlavor))
        {
            Object text = systemClipboard.getData(dataFlavor);
            return (String) text;
        }

        return null;
    }

    private static Clipboard getSystemClipboard()
    {
        Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
        Clipboard systemClipboard = defaultToolkit.getSystemClipboard();

        return systemClipboard;
    }
}

答案 2 :(得分:8)

这是以装饰方式书写的公认答案:

Toolkit.getDefaultToolkit()
        .getSystemClipboard()
        .setContents(
                new StringSelection(txtMySQLScript.getText()),
                null
        );

答案 3 :(得分:4)

对于基于 JavaFx 的应用程序。

        //returns System Clipboard
        final Clipboard clipboard = Clipboard.getSystemClipboard();
        // ClipboardContent provides flexibility to store data in different formats
        final ClipboardContent content = new ClipboardContent();
        content.putString("Some text");
        content.putHtml("<b>Some</b> text");
        //this will be replaced by previous putString
        content.putString("Some different text");
        //set the content to clipboard
        clipboard.setContent(content);
       // validate before retrieving it
        if(clipboard.hasContent(DataFormat.HTML)){
            System.out.println(clipboard.getHtml());
        }
        if(clipboard.hasString()){
            System.out.println(clipboard.getString());
        }

ClipboardContent 可以以多种数据格式(例如,html,url,纯文本,图像)保存多个数据。

有关更多信息,请参见official documentation

答案 4 :(得分:1)

我发现了一种更好的方法,因此您可以从txtbox获取输入或在该文本框中生成某些内容,并且可以单击按钮来执行此操作。以下是复制到剪贴板的代码(2017/2018)!

import java.awt.datatransfer.*;
import java.awt.Toolkit;

private void /* Action performed when the copy to clipboard button is clicked */ {
    String ctc = txtCommand.getText().toString();
    StringSelection stringSelection = new StringSelection(ctc);
    Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
    clpbrd.setContents(stringSelection, null);
}

// txtCommand is the variable of a text box

答案 5 :(得分:0)

供参考:

static void copyToClipboard(String text) {
    java.awt.Toolkit.getDefaultToolkit().getSystemClipboard()
        .setContents(new java.awt.datatransfer.StringSelection(text), null);
}

答案 6 :(得分:-1)

String text = Toolkit.getDefaultToolkit()
        .getSystemClipboard()
        .setContents(
                new StringSelection(Jtextbox1.getText()),
                null
        ); 

System.out.println(text);