使用Java将文本复制到剪贴板 [英] Copying text to the clipboard using Java
本文介绍了使用Java将文本复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将文本从 JTable
的单元格复制到剪贴板,使其可以粘贴到其他程序(如Microsoft Word)中。我有来自 JTable
的文本,但我不确定如何将其复制到剪贴板。
I want to copy text from a JTable
's cell to the clipboard, making it available to be pasted into other programs such as Microsoft Word. I have the text from the JTable
, but I am unsure how to copy it to the clipboard.
推荐答案
这适合我,非常简单:
导入这些:
import java.awt.datatransfer.*;
import java.awt.Toolkit;
然后将这段代码放在你想要改变剪贴板的地方:
And then put this snippet of code wherever you'd like to alter the clipboard:
String myString = "This text will be copied into clipboard when running this code!";
StringSelection stringSelection = new StringSelection(myString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
我刚试过它并且有效。不确定代码中是否存在一些最佳实践缺陷,但它会解决您的问题!
I've just tried it and it works. Not sure if there are some 'best practice' flaws in the code though, but it will solve your problem!
这篇关于使用Java将文本复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文