Copy text to clipboard using java

I want to copy text from JTable cells to the clipboard so that it can be pasted into other programs, such as Microsoft Word I have text from JTable, but I'm not sure how to copy it to the clipboard

Solution

This applies to me and is very simple:

Import these:

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

Then place this code anywhere you want to change the clipboard:

String myString = "This text will be copied into clipboard";
StringSelection stringSelection = new StringSelection(myString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection,null);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>