Java Example Program / Sample Source Code
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class SettingAndGettingText {
public static void main(String[] argv) throws Exception {
StringSelection string = new StringSelection("String is here");
//setting string to Clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(string, null);
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
//getting string from Clipboard
String str = (String) transferable.getTransferData(DataFlavor.stringFlavor);
System.out.println(str);
}
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} |
|
|