I have created a class that handles clipboard changes following a few examples. That works correctly for Strings, I copy a String, I get it from my app and then I can copy it after. When I copy a file or something that it is not a String, I get an exception in gainOwnership->systemClipboard.setContents function. Finally, my clipboard status is lost, so I can not copy the file that I wanted to copy at beginning.
This is my code:
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class ClipboardListener extends Thread implements ClipboardOwner {
Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
public void run() {
Transferable selection = systemClipboard.getContents(this);
gainOwnership(selection);
System.out.println("Listening to board...");
}
public void lostOwnership(Clipboard c, Transferable t) {
Transferable contents = systemClipboard.getContents(this);
processContents(contents);
gainOwnership(contents);
}
void processContents(Transferable t) {
try {
System.out.println("Processing: " + t.getTransferData(DataFlavor.stringFlavor).toString());
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
void gainOwnership(Transferable t) {
try {
this.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
systemClipboard.setContents(t, this);
}
}
I want to capture only the Strings, I don't mine in other objects. But, they have to be accessible into the Clipboard after processing it into my app.
Here is my stackTrace:
Listening to board...
Processing: Eventos.docx
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: createImageUsingNativeSize
at sun.lwawt.macosx.CDataTransferer.getImageForByteStream(Native Method)
at sun.lwawt.macosx.CDataTransferer.platformImageBytesToImage(CDataTransferer.java:238)
at sun.awt.datatransfer.DataTransferer.translateBytes(DataTransferer.java:1659)
at sun.lwawt.macosx.CDataTransferer.translateBytes(CDataTransferer.java:142)
at sun.awt.datatransfer.ClipboardTransferable$DataFactory.getTransferData(ClipboardTransferable.java:71)
at sun.awt.datatransfer.ClipboardTransferable.getTransferData(ClipboardTransferable.java:168)
at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1105)
at sun.lwawt.macosx.CClipboard.setContentsNative(CClipboard.java:79)
at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:107)
at mes.vividox.utils.ClipboardListener.gainOwnership(ClipboardListener.java:50)
at mes.vividox.utils.ClipboardListener.lostOwnership(ClipboardListener.java:36)
at sun.awt.datatransfer.SunClipboard$2.run(SunClipboard.java:311)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
When I try to copy a file this is what I get. I'm in OS X.