标签:boolean java类 操作 事件 etc nts too getc board
//获取系统剪切板 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //设置字符串 String str = "测试设置String到剪切板"; //构建String数据类型 StringSelection selection = new StringSelection(str); //添加文本到系统剪切板 clipboard.setContents(selection, null);
Transferable content = clipboard.getContents(null);//从系统剪切板中获取数据 if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {//判断是否为文本类型 String text = (String) content.getTransferData(DataFlavor.stringFlavor);//从数据中获取文本值 if (text == null) { return; } System.out.println(text); }
public static void setImageClipboard(Image image) { ImageSelection imgSel = new ImageSelection(image); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null); } public static class ImageSelection implements Transferable { private Image image; public ImageSelection(Image image) {this.image = image;} public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[]{DataFlavor.imageFlavor}; } public boolean isDataFlavorSupported(DataFlavor flavor) { return DataFlavor.imageFlavor.equals(flavor); } public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (!DataFlavor.imageFlavor.equals(flavor)) {throw new UnsupportedFlavorException(flavor);} return image; } }
public static Image getImageClipboard() { Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); try { if (null != t && t.isDataFlavorSupported(DataFlavor.imageFlavor)) { Image image = (Image)t.getTransferData(DataFlavor.imageFlavor); return image; } } catch (UnsupportedFlavorException e) { //System.out.println("Error tip: "+e.getMessage()); } catch (IOException e) { //System.out.println("Error tip: "+e.getMessage()); } return null; }
public class TestSelection implements Transferable, ClipboardOwner { public static final DataFlavor rangeFlavor = new DataFlavor(RangeDataModel.class, "Report Range");//class为自定义的java类 字串随便 private static final DataFlavor[] flavors = { rangeFlavor }; private Object data; public TestSelection(Object data) { this.data = data; } @Override public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor.equals(flavors[RANGE])) { return data; } else { throw new UnsupportedFlavorException(flavor); } } @Override public DataFlavor[] getTransferDataFlavors() { return (DataFlavor[]) flavors.clone(); } @Override public boolean isDataFlavorSupported(DataFlavor flavor) { for (int i = 0; i < flavors.length; i++) { if (flavor.equals(flavors[i])) { return true; } } return false; } @Override public void lostOwnership(Clipboard clipboard, Transferable contents) { // TODO Auto-generated method stub } }
//创建自定义剪切板对象 Clipboard clipboard = new Clipboard("clipboardName"); //构建数据对象 data为数据类型java类的对象 ReportSelection content = new ReportSelection(data); clipboard.setContents(content, null);
//注意:自定义剪切板必须给null参数 Transferable content = clipboard.getContents(null); if (content.isDataFlavorSupported(ReportSelection.rangeFlavor)) { RangeDataModel data = (RangeDataModel) content.getTransferData(ReportSelection.rangeFlavor); System.out.println(data); }
Clipboard clipboard = Clipboard.getSystemClipboard(); ClipboardContent cc = new ClipboardContent(); cc.putString("测试使用JavaFX向系统剪切板放置数据"); clipboard.setContent(cc);
Clipboard clipboard = Clipboard.getSystemClipboard();
clipboard.getContent(DataFormat.PLAIN_TEXT);
4.1 Swing操作方式的Clipboard类位于 java.awt.datatransfer包中,而JavaFX操作方式的Clipboard类位于javafx.scene.input包中。
4.2 如果你在你的Java桌面应用程序中同时使用了Swing和JavaFX技术,建议只使用一种剪切方法,且要保证在对应的事件线程上同步,否则可能会遇到剪切板锁定无法访问的问题。
本文参考了以下文章的内容
5.1 http://blog.csdn.net/shenzhennba/article/details/8213294
5.2 http://cuiweiqing1988.iteye.com/blog/947030
转载于:http://www.alanzeng.cn/2015/10/java-clipboard/
标签:boolean java类 操作 事件 etc nts too getc board
原文地址:https://www.cnblogs.com/xiaostudy/p/12909146.html