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

我想复制文本从 JTable的单元格到剪贴板,使其可粘贴到其他程序,如微软 Word。我有从 JTable的文本,但我不确定如何复制到剪贴板。

144217 次浏览

这对我很有用,而且很简单:

导入这些:

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

然后把这段代码放在你想修改剪贴板的地方:

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

下面的类允许您将字符串复制/粘贴到/从剪贴板。

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;


import static java.awt.event.KeyEvent.*;
import static org.apache.commons.lang3.SystemUtils.IS_OS_MAC;


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


int controlKey = IS_OS_MAC ? VK_META : VK_CONTROL;
robot.keyPress(controlKey);
robot.keyPress(VK_V);
robot.keyRelease(controlKey);
robot.keyRelease(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();
return defaultToolkit.getSystemClipboard();
}
}

这是用装饰性的方式写出的公认答案:

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

我找到了一个更好的方法,这样您就可以从 txtbox 中获得输入,或者在该文本框中生成一些内容,并且能够单击按钮来完成这项工作。!

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

对于基于 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、纯文本、图像)保存多个数据。

有关详细信息,请参阅 正式文件

参考资料:

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