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.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
//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());
}