Java has been able to interact well with the system's Clipboard starting with version 1.5.
If you can call the system's Clipboard directly in the program to save the "copy" of the object content, it can be said than the previous idea much better.
The following is an example of a copy,paste operation on a Java.io.File object, although it is not perfect, but it can be used well in the program.
/** Defines the interface for the class used to provide data for the transport operation * dedicated to the Java.io.File type*/ PackageAngel.datatransfer;ImportJava.awt.datatransfer.*;ImportJava.io.*;ImportAngel.*; Public classFiletransferImplementsTransferable {Privatefile file; Publicfiletransfer (file file) { This. file=file;} PublicObject gettransferdata (DataFlavor flavor) {if(isdataflavorsupported (flavor)) {returnfile; } Else { return NULL; } } Publicdataflavor[] Gettransferdataflavors () {return Newdataflavor[] {NewDataFlavor (File.getclass (), Dataflavor.javajvmlocalobjectmimetype)};} Public Booleanisdataflavorsupported (DataFlavor flavor) {returnflavor.ismimetypeequal (dataflavor.javajvmlocalobjectmimetype);} }//in the click "Copy" menu Yes, call:transferable Trans=NewFiletransfer ( This. Getselectedfile ()); Toolkit.getdefaulttoolkit (). Getsystemclipboard (). setcontents (trans,NULL);//returns the current data of the Clipboard clipboard PublicObject GetClipboardData () {Try{Transferable Tran=toolkit.getdefaulttoolkit (). Getsystemclipboard (). getcontents (NULL); if(Tran.isdataflavorsupported (NewDataFlavor (Dataflavor.javajvmlocalobjectmimetype))) {Object obj=tran.gettransferdata (NewDataFlavor (Dataflavor.javajvmlocalobjectmimetype)); if(objinstanceofFile) { return(File) obj; } Else { return NULL; } } Else { return NULL; } } Catch(Exception err) {err.printstacktrace (); return NULL; }}
There is also a feature that allows you to easily monitor changes in the contents of the system Clipboard in a Java program, but if you duplicate the same type of data, such as copying two different content text, only one event is triggered:
// turn on the system Clipboard Listener, which is called when the target clipboard of the listener changes Toolkit.getdefaulttoolkit (). Getsystemclipboard (). Addflavorlistener (this // when the listener's target clipboard changes. public void Flavorschanged (flavorevent e) { try
{transferable Tran
=toolkit.getdefaulttoolkit (). Getsystemclipboard (). getcontents (null ); System.out.println (Tran); catch (Exception err) {Err.pri Ntstacktrace (); } }
Call the system-related clipboard in Java clipboard