我创建了一个按照几个例子处理剪贴板更改的类。这适用于字符串,我复制一个字符串,我从我的应用程序得到它,然后我可以复制它。当我复制一个文件或不是字符串的东西时,我在gainOwnership-> systemClipboard.setContents函数中得到一个例外。最后,我的剪贴板状态丢失了,所以我无法复制我想要在开始时复制的文件。
这是我的代码:
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);
}
}
我想只捕获字符串,我不会在其他对象中捕获。但是,在将其处理到我的应用程序后,它们必须可以在剪贴板中访问。
这是我的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)
当我尝试复制文件时,这就是我得到的。我在OS X.