3 Replies Latest reply: Sep 20, 2010 3:28 PM by 843807 RSS

    Java Drag and Drop on Windows Vista/7

    843807
      hi! im trying to get simple drag and drop behaviour on windows vista and windows 7 but it seems it doesnt work. im trying to drag an image file into a JFormattedTextField that has default DnD behaviour so i can get the path on the field. im using a very simple piece of code. it works on ubuntu linux, but it seems that the same code just dont work on windows 7 and windows vista. heres the code

      public class DnDTest{
      public static void main(String[] args){
      JFrame frame = new JFrame();
      JFormattedTextField field = new JFormattedTextField();
      field.setPreferredSize(new Dimension(100,30));
      frame.getContentPane().add(field);
      frame.pack();
      frame.setVisible(true);
      }
      }

      i run it, then drag an image from windows desktop on the field component but the drop doesnt happen. when i do the same on ubuntu it works. im running it from netbeans 6.9 and using jdk1.6_20. thanks in advance!
        • 1. This Thread is now moved
          DarrylBurke
          Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
          • 2. Re: Java Drag and Drop on Windows Vista/7
            843807
            I think you will need a DropTargetListener for Windows Systems - see example:
            import java.awt.*;
            import javax.swing.*;
            import java.awt.dnd.*;
            import java.awt.datatransfer.*;
            import java.io.*;
            
            class DnDTest
            {
                public static void main(String[] args)
                {
                    JFrame frame = new JFrame();
                    JFormattedTextField field = new JFormattedTextField();
            
                    new DropTarget(field, new TextDropTargetListener(field));
            
                    field.setPreferredSize(new Dimension(100,30));
                    frame.getContentPane().add(field);
                    frame.pack();
                    frame.setVisible(true);
                }
            }
            
            class TextDropTargetListener implements DropTargetListener
            {
                public TextDropTargetListener(JTextField aTextField)
                {
                    textField = aTextField;
                }
            
                public void dragEnter(DropTargetDragEvent event)
                {
                    if(!isDragAcceptable(event))
                    {
                        event.rejectDrag();
                        return;
                    }
                }
            
                public void dragExit(DropTargetEvent event)
                {
                }
            
                public void dragOver(DropTargetDragEvent event)
                {
                }
            
                public void dropActionChanged(DropTargetDragEvent event)
                {
                    if(!isDragAcceptable(event))
                    {
                        event.rejectDrag();
                        return;
                    }
                }
            
                public void drop(DropTargetDropEvent event)
                {
                    if(!isDropAcceptable(event))
                    {
                        event.rejectDrop();
                        return;
                    }
            
                    event.acceptDrop(DnDConstants.ACTION_COPY);
            
                    Transferable transferable = event.getTransferable();
                    DataFlavor[] flavors = transferable.getTransferDataFlavors();
                    DataFlavor d = flavors[0];
                    try
                    {
                        if(d.equals(DataFlavor.javaFileListFlavor))
                        {
                            java.util.List<File> fileList = (java.util.List<File>) transferable.getTransferData(d);
                            textField.setText(fileList.get(0).toString());
                        }
                        else if(d.equals(DataFlavor.stringFlavor))
                        {
                            String s = (String) transferable.getTransferData(d);
                            textField.setText(s);
                        }
                    }
                    catch(Exception e)
                    {
                        textField.setText(e.toString());
                    }
                    event.dropComplete(true);
                }
            
                public boolean isDragAcceptable(DropTargetDragEvent event)
                {
                    return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
                }
            
                public boolean isDropAcceptable(DropTargetDropEvent event)
                {
                    return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
                }
            
                private JTextField textField;
            }
            Of course you would need to modify this for multiple selections

            That is how I usually deal with DnD files
            • 3. Re: Java Drag and Drop on Windows Vista/7
              843807
              it worked! thanks a lot, its just a little weird that you can get default drag and drop behaviour on linux but not in windows. thanks again!