Copy and paste string to and from System Clipboard
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
}
private void copy() {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
String text = "value";
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
}
private void paste() {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
DataFlavor flavor = DataFlavor.stringFlavor;
if (clipboard.isDataFlavorAvailable(flavor)) {
try {
String text = (String) clipboard.getData(flavor);
System.out.println(text);
} catch (UnsupportedFlavorException e) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}
}
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
System:
- Copy and paste string to and from System Clipboard
- Copy and paste image to and from System Clipboard
- Alert when Losting Clipboard owner
- System beeper
- Beep by printing the ASCII Bell character to the console
- Total amount of memory in Java virtual machine
- Maximum amount of memory in Java virtual machine
- Free amount of memory in Java Virtual Machine
- Environment variables