Drag a file from Windows and drop it in a client

I would like to have a component on my screen that allows a user to drag a file from Windows into it, and then it will record the filepath. I haven’t found a component that allows for file dropping, so I’m unsure how to proceed. If it matters, in this case the files would always be a PDF, but I could see this being used for other things later.

If there is not a component that has this function built in, hack jobs are accepted :slight_smile:

We have a component called the ‘File Explorer’ which can list files in a given root directory and has a property showing the path that the operator has selected. This may not be exactly what you are looking for, but perhaps worth looking into using.

Definitely worth a look, thank you for the suggestion.

Put a container or label on the screen. You will be dragging a file / files onto this component.
Put a button on the screen with the following code.

[code]from java.awt.dnd import DropTargetListener,DnDConstants,DropTarget

class MyDragDropListener(DropTargetListener):
def drop(self,e):
e.acceptDrop(DnDConstants.ACTION_COPY)
transferable = e.getTransferable()
flavors = transferable.getTransferDataFlavors()
for flavor in flavors:
if (flavor.isFlavorJavaFileListType()):
files = transferable.getTransferData(flavor)
for f in files:
print "File path is: ", f.getPath()
e.dropComplete(True)

comp = event.source.parent.getComponent(‘Container’)

myDragDropListener = MyDragDropListener()

DropTarget(comp, myDragDropListener)[/code]

2 Likes

Thanks! This is the kind of thing I was hoping to get. Looks like it is working and I can modify it for my application.