Call the system-related clipboard in Java clipboard

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.