Skip to main content

Asynchronous drag n drop

4 replies [Last post]
Joined: 2006-05-05


I found the following comments in the java Drag-n-Drop tutorial:

<br />
        //A real application would load the file in another<br />
        //thread in order to not block the UI.  This step<br />
        //was omitted here to simplify the code.

However, I have tried to do exactly that and it does not seem possible. I doubt the person who wrote this comment actually tried moving the import into another thread.

It seems like Transferable.getTransferData() works OK when I call it within TransferHandler.importData(JComponent c, Transferable t), but when I try to make importData() a non-blocking method (i.e., perform the import by launching another thread), then I always end up with the following exception:

<br />
java.awt.dnd.InvalidDnDOperationException: No drop current<br />
at sun.awt.dnd.SunDropTargetContextPeer.getTransferData(<br />
at sun.awt.datatransfer.TransferableProxy.getTransferData(<br />
at java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(

I think this exception makes sense. It is saying that when user drops something, we need to handle it at that moment. It implies that we cannot wait an arbitrarily long time (like days or weeks) before we handle the drop.


(1) is Transferable.getTransferData() a long-running task?
(2) If the answer to (1) is yes, then how do I call getTransferData() without blocking the EDT thread?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2007-05-03

I really don't know what are you talking about :)

Cause I've to learn stuff about dnd but.

If the problem is that if you try to do this on another thread that is not really the problem
the problem is that you get the error.

I don't know how this is done really, but if you block the thread doing the execution check ( the one that gives u error) then perform the stuff u need on another thread and then signal the stopped one to wake cause everithing is done then you could recover the state and would not get the error I suppose.

Hope this helps,

Joined: 2005-05-31

This example is about dropping files into an application to open them. The comment is referring only to loading the file in another thread, not processing the entire drop in another thread. That is, call getTransferData() to get the file list, start up another thread to load the files, and then return from the importData method.


Joined: 2006-11-09

Am facing a similar problem. I've been using swing workers for all my long running tasks but in this situation, a swing worker will not work because it is asynchronous. In other words, the import method can't return a valid true or false status because the worker runs on a separate thread. I could always return true but this would be wrong if the worker encounters an exception.

It looks like this is one situation where I must use a synchronous worker like spin or foxtrot.

Joined: 2004-07-26

There might be a way you can defer completing the drop until you've extracted the transfer data.

See as one alternative; you might be able to do which effectively suspends the current EDT processing and starts a new event pump for the duration of the data extraction task. When the extraction finishes, the original dispatching resumes.