Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Java Drag and Drop on Windows Vista/7

843807
Member Posts: 46,582
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!
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
-
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.
-
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 -
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.