Java Operating system Clipboard content data

Source: Internet
Author: User
Customers have a variety of curious needs, is now required to use the platform in the process of the copied content to save and display for the use of people reference
  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 2012-7-12 11:33:06
  14. * @param args
  15. */
  16. public static void Main (string[] args) {
  17. Clipboard clip = Toolkit.getdefaulttoolkit (). Getsystemclipboard ();//Get 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. * Get text content from a specified clipboard
  27. * Local clipboard using Clipborad CP = new Clipboard ("clip1"); To construct
  28. * System Clipboard using Clipboard SYSC = Toolkit.getdefaulttoolkit (). Getsystemclipboard ();
  29. * The contents of the Clipboard getcontents (null); Back to transferable
  30. */
  31. protected static String Getclipboardtext () throws exception{
  32. Clipboard clip = Toolkit.getdefaulttoolkit (). Getsystemclipboard ();//Get system Clipboard
  33. Get the contents of the Clipboard
  34. Transferable clipt = clip.getcontents (null);
  35. if (clipt! = null) {
  36. Check whether the content is 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. Reading an image from the Clipboard
  48. public static Image Getimagefromclipboard () throws exception{
  49. Clipboard SYSC = Toolkit.getdefaulttoolkit (). 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 an 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.getdefaulttoolkit (). Getsystemclipboard (). setcontents (trans, NULL);
  73. }
  74. }
Copy Code
  1. Package Cn.net.ssd.common.format;
  2. /*
  3. * View the graphics 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.WindowEvent;
  12. /**
  13. * A window for displaying graphics
  14. */
  15. public class Imageviewer extends Frame {
  16. private image Image;
  17. /**
  18. * Show 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. Window adapts to 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 images
  47. */
  48. public static Image Getimagefromfile (String fileName) {
  49. Toolkit Toolkit = Toolkit.getdefaulttoolkit ();
  50. Image image = Toolkit.getimage (fileName);
  51. return image;
  52. }
  53. }
Copy Code
  • 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.