• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with Invalid Drag and Drop Exception  RSS feed

 
Gary W. Lucas
Ranch Hand
Posts: 67
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm stuck on an implementation that uses the TransferHandler class and was wondering if anyone could point out what I'm doing wrong...

I am trying to implement an application where I use drag an file from Windows explorer and drop it onto an application for processing. I posted an issue related to this last year at Change Icon Label for Drag and Drop but kind of struck out. This morning I spotted Oracle's Tutorial Top Level Drop which offered an alternate approach that seemed to solve my problem. Unfortunately, Oracle's tutorial is full of lots of extra stuff which is totally extraneous to what I want to do. So I tried stripping the example down and running it. When I do, it works beautifully in terms of dragging the image onto the application user-interface, but when I release the mouse button, I get the following execption




Clearly I missed something, but I've been through the code and did a web search, and haven't been able to figure out what's wrong. Does anyone have a solution?

Thanks.

Here's my source code.

 
Gary W. Lucas
Ranch Hand
Posts: 67
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this was no fun at all, but I've got a solution. It's a feature, not a bug.

First off, there was a key difference between the application I had in mind, and the example that Oracle provided. The "Top Level Drop" example was designed to accept any kind of file that a user might drag onto the application frame. My application was targeted to a specific file type (in my case, graphics files, but it could have been anything). So it had to do something that Top Level Drop didn't do and inspect the file name in the canImport method. To do so, it needed to call getTransferData() to get the list of files that would be transferred in. I would then inspect these and have canImport() return true or false depending on whether the transfer list contained files of interest. Unfortunately, getTransferData() was basically guaranteed to throw the exception I was seeing.

Under windows, the Transferable instance has data as long as the user is holding down the mouse button. When he releases it, Java/Swing will call the canImport() method one last time... But when it does, the getTransferData() method will not have valid data. And, worse, if getTransferData() is called, Java will throw the InvalidDnDOperationException that presented such a problem in my earlier example. Ironically, the very next thing that happens is that Java/Swing calls importData(). And, when it does, the transfer data will be available just fine.

So the key is to simply catch and ignore the exception, returning "true" as the status of the call. Yeah, Swing's behavior is a bit glitchy in this case. But at least there's a work around.

Here's an example of the test program. It's been modified so that it only accepts image files (and rejects others).


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!