Java access (copy, paste) clipboard

Source: Internet
Author: User

A total of 4 functions are as follows:
1. Get text from the Clipboard.
2. Copy the string to the Clipboard.
3. Get the picture from the Clipboard.
4. Copy the picture to the Clipboard.
Java code
  1. /**
  2. * Get text from the Clipboard.
  3. */
  4. public static String Getsysclipboardtext () {
  5. String ret = "";
  6. Clipboard Sysclip = Toolkit.getdefaulttoolkit (). Getsystemclipboard ();
  7. //Get the contents of the Clipboard
  8. Transferable CLIPTF = sysclip.getcontents (null);
  9. if (CLIPTF! = null) {
  10. //Check whether the content is of text type
  11. if (cliptf.isdataflavorsupported (Dataflavor.stringflavor)) {
  12. try {
  13. ret = (String) CLIPTF
  14. . Gettransferdata (Dataflavor.stringflavor);
  15. } catch (Exception e) {
  16. E.printstacktrace ();
  17. }
  18. }
  19. }
  20. return ret;
  21. }
  22. /** 
  23. * Copy the string to the Clipboard.
  24. */
  25. public static void Setsysclipboardtext (String writeme) {
  26. Clipboard clip = Toolkit.getdefaulttoolkit (). Getsystemclipboard ();
  27. Transferable Ttext = new StringSelection (Writeme);
  28. Clip.setcontents (Ttext, null);
  29. }
  30. /** 
  31. * Get pictures from the Clipboard.
  32. */
  33. public static Image Getimagefromclipboard () throws Exception {
  34. Clipboard SYSC = Toolkit.getdefaulttoolkit (). Getsystemclipboard ();
  35. Transferable cc = sysc.getcontents (null);
  36. if (cc = = null)
  37. return null;
  38. Else if (cc.isdataflavorsupported (dataflavor.imageflavor))
  39. return (Image) cc.gettransferdata (Dataflavor.imageflavor);
  40. return null;
  41. }
  42. /** 
  43. * Copy the picture to the Clipboard.
  44. */
  45. public static void Setclipboardimage (final image image) {
  46. Transferable trans = New transferable () {
  47. Public dataflavor[] Gettransferdataflavors () {
  48. return new dataflavor[] {dataflavor.imageflavor};
  49. }
  50. Public Boolean isdataflavorsupported (DataFlavor flavor) {
  51. return DataFlavor.imageFlavor.equals (flavor);
  52. }
  53. Public Object Gettransferdata (dataflavor flavor)
  54. throws Unsupportedflavorexception, IOException {
  55. if (isdataflavorsupported (flavor))
  56. return image;
  57. throw New unsupportedflavorexception (flavor);
  58. }
  59. };
  60. Toolkit.getdefaulttoolkit (). Getsystemclipboard (). setcontents (trans,
  61. null);
  62. }
Transferred from: http://javapub.iteye.com/blog/939167

Java access (copy, paste) clipboard (GO)

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.