This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

Java Drag and Drop on Windows Vista/7

843807
843807 Member Posts: 46,582
edited Sep 20, 2010 4:28PM in Abstract Window Toolkit (AWT)
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!

Comments

  • darrylburke
    darrylburke Member Posts: 18,007
    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.
  • 843807
    843807 Member Posts: 46,582
    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
  • 843807
    843807 Member Posts: 46,582
    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!
This discussion has been closed.