Java operating system clipboard content data

Source: Internet
Author: User
Java operating system clipboard content data
The customer has a variety of rare requirements and now needs to save and display the copied content during the use of the platform for reference by the user.

  1. Package cn.net. ssd. common. format;
  2. Import java. awt. Image;
  3. Import java. awt. Toolkit;
  4. Import java. awt. datatransfer. Clipboard;
  5. Import java. awt. datatransfer. DataFlavor;
  6. Import java. awt. datatransfer. StringSelection;
  7. Import java. awt. datatransfer. Transferable;
  8. Import java. awt. datatransfer. UnsupportedFlavorException;
  9. Import java. io. IOException;
  10. Public class ClipboradOperate {
  11. /** @ Description:
  12. * @ Author zhk
  13. * @ Createtime 11:33:06 am
  14. * @ Param args
  15. */
  16. Public static void main (String [] args ){
  17. Clipboard clip = Toolkit. getdefatooltoolkit (). getSystemClipboard (); // Obtain the system Clipboard
  18. Try {
  19. ImageViewer im = new ImageViewer (getImageFromClipboard ());
  20. } Catch (Exception e ){
  21. // TODO Auto-generated catch block
  22. E. printStackTrace ();
  23. }
  24. }
  25. /**
  26. * Obtain text content from the specified clipboard
  27. * The local Clipboard is constructed using Clipborad cp = new Clipboard ("clip1 ");
  28. * The system Clipboard uses Clipboard sysc = Toolkit. getdefatooltoolkit (). getSystemClipboard ();
  29. * The content of the clipboard is getContents (null); the returned value is Transferable.
  30. */
  31. Protected static String getClipboardText () throws Exception {
  32. Clipboard clip = Toolkit. getdefatooltoolkit (). getSystemClipboard (); // Obtain the system Clipboard
  33. // Obtain the content in the clipboard
  34. Transferable clept = clip. getContents (null );
  35. If (clept! = Null ){
  36. // Check whether the content is of the text type
  37. If (clipT. isDataFlavorSupported (DataFlavor. stringFlavor ))
  38. Return (String) clipT. getTransferData (DataFlavor. stringFlavor );
  39. }
  40. Return null;
  41. }
  42. // Write text data to the clipboard
  43. Protected static void setClipboardText (Clipboard clip, String writeMe ){
  44. Transferable tText = new StringSelection (writeMe );
  45. Clip. setContents (tText, null );
  46. }
  47. // Read the image from the clipboard
  48. Public static Image getImageFromClipboard () throws Exception {
  49. Clipboard sysc = Toolkit. getdefatooltoolkit (). getSystemClipboard ();
  50. Transferable cc = sysc. getContents (null );
  51. If (cc = null)
  52. Return null;
  53. Else if (cc. isDataFlavorSupported (DataFlavor. imageFlavor ))
  54. Return (Image) cc. getTransferData (DataFlavor. imageFlavor );
  55. Return null;
  56. }
  57. // Write the image to the clipboard
  58. Protected static void setClipboardImage2 (final Image image ){
  59. Transferable trans = new Transferable (){
  60. Public DataFlavor [] getTransferDataFlavors (){
  61. Return new DataFlavor [] {DataFlavor. imageFlavor };
  62. }
  63. Public boolean isDataFlavorSupported (DataFlavor flavor ){
  64. Return DataFlavor. imageFlavor. equals (flavor );
  65. }
  66. Public Object getTransferData (DataFlavor flavor) throws UnsupportedFlavorException, IOException {
  67. If (isDataFlavorSupported (flavor ))
  68. Return image;
  69. Throw new UnsupportedFlavorException (flavor );
  70. }
  71. };
  72. Toolkit. getdefatooltoolkit (). getSystemClipboard (). setContents (trans, null );
  73. }
  74. }

  1. Package cn.net. ssd. common. format;
  2. /*
  3. * Display the graph window
  4. */
  5. Import java. awt. Frame;
  6. Import java. awt. Graphics;
  7. Import java. awt. Image;
  8. Import java. awt. MediaTracker;
  9. Import java. awt. Toolkit;
  10. Import java. awt. event. WindowAdapter;
  11. Import java. awt. event. javaswevent;
  12. /**
  13. * Window used to display graphics
  14. */
  15. Public class ImageViewer extends Frame {
  16. Private Image image;
  17. /**
  18. * Display an image
  19. * @ Param viewMe
  20. */
  21. Public ImageViewer (Image viewMe ){
  22. Image = viewMe;
  23. MediaTracker mediaTracker = new MediaTracker (this );
  24. MediaTracker. addImage (image, 0 );
  25. Try {
  26. MediaTracker. waitForID (0 );
  27. } Catch (InterruptedException ie ){
  28. Ie. printStackTrace ();
  29. System. exit (1 );
  30. }
  31. AddWindowListener (new WindowAdapter (){
  32. Public void windowClosing (WindowEvent e ){
  33. System. exit (0 );
  34. }
  35. });
  36. // Adapt the window to the image size
  37. SetSize (image. getWidth (null), image. getHeight (null ));
  38. // Window title
  39. SetTitle ("Viewing Image from Clipboard ");
  40. SetVisible (true );
  41. }
  42. Public void paint (Graphics graphics ){
  43. Graphics. drawImage (image, 0, 0, null );
  44. }
  45. /**
  46. * Used to read Image files and generate Image objects
  47. */
  48. Public static Image getImageFromFile (String fileName ){
  49. Toolkit toolkit = Toolkit. getdefatooltoolkit ();
  50. Image image = toolkit. getImage (fileName );
  51. Return image;
  52. }
  53. }

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.