Use the Toolkit
to get the system clipboard. Create a StringSelection
with the String
and add it to the Clipboard
.
Simplified:
StringSelection selection = new StringSelection(theString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);