Ok so to learn Dnd image ability a little further I decided to follow the advice of some very nice people here and created a simple drag and drop application trying to use an external image when trying to drag and drop. Unfortunatly I am still getting errors during the startDrag method call. This is the error message it gives "java.awt.dnd.InvalidDnDOperationException: drag image creation problem: drag image is not ready"
The code is as follows:
The code the error happens on is:
I see the text Drag Started in my console with the sysout line but it will not show the image I am trying to use during the drag operation.
Any help or guidance on this issue would be greatly appreciated.
Thank you for your help and comments. I did do some testing and loaded the image during the constructor but that still did not work so I've been looking over the link you provided and they do some interesting things there like after they get the image they load it into a bufferedimage.
What I'm really confused about is the code you posted it uses this little piece of code early on:
And I don't see that method being overridden anywhere in the code so is this method appart of the startDrag class? I think I'm really close to understanding and getting this down "I Hope" but it's still a little out of reach.
It's defined in its superclass. You can navigate by clicking on the imported class and then at the top bar select "source".
Edit: I'm not an expert about images and image loading but I remember that Image is loaded lazy-mode, so it is created only just before the first draw (That's probably why width and height are set to -1). Maybe the usage of a buffered image would be of help.
Hmmm I'm still confused. I changed the constructor to build a bufferedimage. Shown below:
And then I click and drag on my JPanel starting the following method:
It starts the drag operation but still does not display the simple image I had provided... Do I need to just program out the entire dnd function to make something work? I get no error's at all now which I guess is a plus but I don't see a drag image at all. I've read over the superclass and still find nothing that would prevent the image from being shown.
Here is the output of my sysout line during the start of the drag.
Any help on helping me understand what is going on I would greatly appreciate the help and time.
You still does not implemented some basic methods like getTransferDataFlavors and getTransferData. But you have overridden default drag cursor (don't do that untill you get other things working). So it's not clear if drag started at all. Get the drag and drop working (even without an image) first. You may use really simple flavor like stringFlavor.
Anyway, that may or may not be the problem. Which value does DragSource.isDragImageSupported() return? On my platform it returns false, so i cannot debug any issues related to the drag image (seems that javafx apps support drag icon but it's another story).
Thanks I'll give it a go and get back. Also I didn't feel those other methods were needed as I'm just starting the Drag operating and doing nothing with it yet. It does definatly start the drag because the icon was changing when I started the drag before I took it out. Either way I'll try and use what you said and see where I get.
I know this thread is old but I forgot to post the solution. Below is the function that works correctly with windows 7 and above but not windows xp. I believe most of the problems I was having was with windows xp.
I took this code from somewhere when I was testing and modified it to meet my needs.