import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.StringTokenizer;
public class MainClass {
public static void main(String args[]) {
// void copyToClipboard() {
String toClipboard = "Hello from Java!";
StringSelection ss = new StringSelection(toClipboard);
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
clip.setContents(ss, ss);
// Paste
clip = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clip.getContents(new MainClass().getClass());
if (contents == null)
System.out.println("The clipboard is empty.");
else {
if (contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
String data = (String) contents.getTransferData(DataFlavor.stringFlavor);
if (data == null)
System.out.println("null");
else {
StringTokenizer st = new StringTokenizer(data, "\n");
while (st.hasMoreElements())
System.out.println(st.nextToken());
}
} catch (IOException ex) {
System.out.println("IOException");
} catch (UnsupportedFlavorException ex) {
System.out.println("UnsupportedFlavorException");
}
} else
System.out.println("Wrong flavor.");
}
}
}
|