// If an image is on the system clipboard, this method returns it;
// otherwise it returns null.
public static Image getClipboard() {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); try {
if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
Image text = (Image)t.getTransferData(DataFlavor.imageFlavor);
return text;
}
} catch (UnsupportedFlavorException e) {
} catch (IOException e) {
}
return null;
}

Setting an image on the system clipboard requires a custom Transferable object to hold the image while on the clipboard.

    // This method writes a image to the system clipboard.
// otherwise it returns null.
public static void setClipboard(Image image) {
ImageSelection imgSel = new ImageSelection(image);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
} // This class is used to hold an image while on the clipboard.
public static class ImageSelection implements Transferable {
private Image image; public ImageSelection(Image image) {
this.image = image;
} // Returns supported flavors
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.imageFlavor};
} // Returns true if flavor is supported
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
} // Returns image
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (!DataFlavor.imageFlavor.equals(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
return image;
}
}
Related Examples

最新文章

  1. emoji表情引发的JNI崩溃
  2. SQLServer学习笔记<>sql的范围内查找,sql数据类型,字符串处理函数
  3. Xcode中的几个常用文件路径
  4. 获得servletContext的路径的方法
  5. XML学习总结
  6. Android学习第一课
  7. 新版本的tlplayer for android ,TigerLeapMC for windows发布了
  8. C与C++的区别
  9. 20170422早会训话,ps:程序出现两次BUG,领导很生气
  10. utc时间转成local时间
  11. 写入多线程Log4net 多线程写入
  12. leveldb 学习记录(一) skiplist
  13. Git与GitHub学习笔记(四)合并远程分支
  14. p1654 OSU!
  15. FoxMail提示:请求的名称有效,但是找不到请求的类型的数据
  16. java怎样获得JVM可能的总内存,最大内存,以及空暇内存?
  17. Mysql 中创建索引和索引的使用问题
  18. 最新PHPcms9.6.0 任意文件上传漏洞
  19. 《Programming with Objective-C》第五章 Customizing Existing Classes
  20. Python学习札记(十七) 高级特性3 列表生成式

热门文章

  1. unity5 Text
  2. linux下查看doc在线帮助文件
  3. C# FUNC 应用
  4. android购物车遇到的问题
  5. 进程的处理器亲和性和 vCPU 的绑定(查看cpu信息,超线程等)
  6. iOS_20_微博Dock的尾随切换
  7. vue的面包屑导航组件
  8. Eclipse下maven部署web项目到tomcat7(兼容tomcat8)
  9. StructureMap
  10. java.util.logging.Logger日志生成过程浅析 (转)