I am trying to get some text from the clipboard using this method, but it throws an exception into the string instead of the text.
Am I just doing it wrong or something?
Transferable t = cb.getContents(null);
String begin = t.toString();
System.out.println("Successfully fetched:");
System.out.println(begin);
Throws this error:
sun.awt.datatransfer.ClipboardTransferable@6d03e736
Try this code snippet which attempts to retrieve a String
from the System clipboard:
String result = "";
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
boolean hasStringText = (contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
if (hasStringText) {
try {
result = (String)contents.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException | IOException ex) {
System.out.println(ex); ex.printStackTrace();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.