Java Drag and Drop an Image and Show the image thumbnail while dragging

两盒软妹~` 提交于 2019-12-10 10:27:51

问题


I have a grid layout with image nodes and I want to add in my application the dnd functionality, for example when I drag and drop an image node to an target JPanel make some actions for example show in original size, delete etc. I have achieve this with to ways one with transferhandler and one with the DropTargets, DragSource etc.But I have two questions and I want to discuss them with you.

a)Which of the two ways it's better to use? I understand that the second way give me more features than the TransferHandler and the first it's simplest to use. But I can do my work both of these ways, there is a more effective of two, or it's all about what I prefer to code.

b)While the dragging occurs I want to display an image near the cursor. The first though I make is to use the glassPane of the JFrame and create a JCompoment which paints proper image and follow the cursor.Can I make it with this way? After this I see in the documentation that the startDrag method of the DragSource get an dragImage argument, is this for the reason I want, I try to pass an Image but I not see any results.

--------------UPDATE---------------------

For the second question (b) I tried with the glasspane but I have problem in the interworking between java DND and my listeners for display the image thumb in the glasspane. each time only one of them work properly.

!!! More Specifically the mouseDragged event not fired when I have already call the startDrag() method

Thank you for your time.


回答1:


Solved

Finally, I find the solution myself.

If anyone has the same problem I say that the point is that after the startDrag() method fired all the mouseDraged events goes to dragMouseMoved()(from DragSourceMotionListener) and I make the error to use also the "void mouseDragged(MouseEvent e)" from the MouseMotionListener.



来源:https://stackoverflow.com/questions/4291089/java-drag-and-drop-an-image-and-show-the-image-thumbnail-while-dragging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!