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 GetTransferData_StringSelection {
public static void main(String[] argv) throws Exception {
StringSelection string = new StringSelection("String is here");
DataFlavor[] transferDataFlavors = string.getTransferDataFlavors();
//setting string to Clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(string, null);
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
string.lostOwnership(Toolkit.getDefaultToolkit().getSystemClipboard(), transferable);
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();
}
}
} |
|
|