title: Java 7: How to implement drag & drop in Java?
body: <p>I am currently experimenting with drag&amp;drop using Java 7 Update 21.</p>
  
  <p>My target operating systems are:</p>
  
  <ul>
  <li>Windows 7</li>
  <li>Ubuntu 12.04</li>
  <li>Mac OSX 10.6 / 10.8</li>
  </ul>
  
  <p>The requirements are:</p>
  
  <ul>
  <li><p>drag files from the filesystem and drop it to my Java application (making a
  copy of the file to a temporary directory) -> works for Linux &amp; MacOSX &amp; Windows</p></li>
  <li><p>drag e-mails from Thunderbird and drop them to my Java application (saving
  them as complete *.eml file on the filesystem)</p></li>
  </ul>
  
  <p>The following code works with simple file drops to my application for Windows, MacOSX <strong>and</strong> Ubuntu. A further requirement is to drop e-mails from Thunderbird to my Java application (the mail is automatically converted to an *.eml file and stored to disk). This also works fine for Windows but I get a <strong>"Data Flavor not supported exception"</strong> in Ubuntu and MacOSX...</p>
  
  <p>EDIT: I tried it with OpenJDK 7 on Ubuntu, but with that, even normal file drops doesn't work. Only with the JDK version of Oracle.</p>
  
  <p>Does somebody has an idea how to fix / achieve that?</p>
  
  <p>Many thanks in advance!</p>
  
  <p>Here's a simple executeable sample:</p>
  
  <pre><code>import java.awt.datatransfer.DataFlavor;
  import java.awt.datatransfer.Transferable;
  import java.awt.datatransfer.UnsupportedFlavorException;
  import java.awt.dnd.DnDConstants;
  import java.awt.dnd.DropTarget;
  import java.awt.dnd.DropTargetDropEvent;
  import java.io.File;
  import java.io.IOException;
  import java.nio.file.Files;
  import java.nio.file.Paths;
  import java.nio.file.StandardCopyOption;
  import java.util.List;
  
  
  public class DragDropTest extends javax.swing.JFrame {
  
  
      public DragDropTest() {
          initComponents();
          initDragAndDrop();
      }
  
      private void initDragAndDrop() {
          this.setDropTarget(new DropTarget(){
              @Override
              public synchronized void drop(DropTargetDropEvent dtde) {
                  try {
                      Transferable transfer = dtde.getTransferable();
                      if(transfer.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                          List objects = (List)transfer.getTransferData(DataFlavor.javaFileListFlavor);
                          for(Object object : objects) {
                              if(object instanceof File) {
                                  File source = (File)object;
                                  File dest = new File(System.getProperty("user.home")+File.separator+source.getName());
                                  Files.copy(Paths.get(source.getAbsolutePath()), Paths.get(dest.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
                                  System.out.println("File copied from "+source.getAbsolutePath()+" to "+dest.getAbsolutePath());
                              }
                          }
                      } else if(transfer.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                          dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                          String type = (String)transfer.getTransferData(DataFlavor.stringFlavor);
                          System.err.println("Data flavor not supported: "+type);
                      } else {
                          System.err.println("Data flavor not supported.");
                      }
                  } catch(UnsupportedFlavorException ex) {
                      System.err.println(ex.getMessage());
                  } catch(IOException ex) {
                      System.err.println(ex.getMessage());
                  } catch(Exception ex) {
                      System.err.println(ex.getMessage());
                  } finally {
                      dtde.dropComplete(true);
                  }
              }
          });
      }
  
      @SuppressWarnings("unchecked")                      
      private void initComponents() {
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
          setTitle("Drag &amp; Drop");
          setResizable(false);
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 200, Short.MAX_VALUE)
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGap(0, 200, Short.MAX_VALUE)
          );
  
          pack();
      }                       
  
      public static void main(String args[]) {
          new DragDropTest().setVisible(true);
      }
  
  }
  </code></pre>