The tricky part is casting java.util.Arrays$ArrayList (object) to List
package com.test; import java.applet.Applet; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.swing.BoxLayout; import javax.swing.JButton; public class MyApplet extends Applet { /** * */ private static final long serialVersionUID = 3253065390977690287L; Clipboard clipboard = getToolkit ().getSystemClipboard (); public MyApplet() { setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); JButton btnNewButton = new JButton("New button"); btnNewButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { getContentFromClipboard(); } }); add(btnNewButton); } private void getContentFromClipboard() { for(DataFlavor dfv : clipboard.getAvailableDataFlavors()) { System.out.println(dfv.getHumanPresentableName() + "\t" + dfv.getMimeType() + "\t" + dfv.getRepresentationClass()); Transferable transferable = clipboard.getContents(this); System.out.println(transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)); if(transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { try { Object object = transferable.getTransferData(dfv); List
stuff = new ArrayList (); stuff = (List ) object; for (File file : stuff) { System.out.println(file.getAbsolutePath()); // Do whatever you want with the file // ... } } catch (UnsupportedFlavorException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } }
Thursday, November 24, 2011
Java Clipboard Copy File
Subscribe to:
Posts (Atom)