Copying to the clipboard in Java [duplicate]

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

Leave a Comment