my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Drag and drop images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drag and drop images" Watch "Drag and drop images" New topic
Author

Drag and drop images

Tim Sangster
Greenhorn

Joined: Mar 27, 2011
Posts: 12
I am looking for a good way to drag and drop images with (changeable) text on them across a panel.
Would there be any way to drag JPanel's around the screen with the mouse and have them stay on the spot they were released? (not snap into something like a gridlayout)
The panels should be fully visible while dragged. So I don't think I can use the drag gable interface.
Currently I am just using an extended JPanel but I am only now implementing the changeable text and JPannels give a lot of flexibility to add more JLabels later.
Would it be possible to make these draggable Panels and should I start looking in extending a JPanel or a layout?
Thank you for any help!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Here's a link to the start of Oracle's tutorial about drag-and-drop: Introduction to DnD. (Acquired via Google keywords "java drag and drop tutorial".) It goes on for about 20 pages; it isn't a simple thing so don't expect to have working code within the hour!
Tim Sangster
Greenhorn

Joined: Mar 27, 2011
Posts: 12
Paul Clapham wrote:Here's a link to the start of Oracle's tutorial about drag-and-drop: Introduction to DnD. (Acquired via Google keywords "java drag and drop tutorial".) It goes on for about 20 pages; it isn't a simple thing so don't expect to have working code within the hour!

Thank you for the reply, however I looked into this and it does not seem to be the kind of drag and drop I am looking for.
I need the kind of drag and drop you find in uml tool's, you press the mouse on a button the image and an image appears under the mouse, you then drag the image to where you want it and release it there.
I am sorry if this is also included in the Oracle dnd demo but I overlooked it.
Are there any existing interfaces for that kind of dnd?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1083
    
  10

Your question is contradictory. Initially, you mentioned about dragging a JPanel - then, you say you need something like that in UML tools.
I think what you want is to actually 'drop' something into a JPanel. So, in that case, you should propably have a JLabel or a JToggleButton with an image on it. Then, you would drag this component and drop it into the JPanel.

The examples given in the tutorial use StringSelection which is to transfer String data - so, in your case you should write a class that implements Transferable - this class should support DataFlavor.imageFlavor and the getTransferData method should return an Image object. Not an easy one to write though.

Finally, based on what to do on the 'drop' action - if you just want to add a JLabel to the panel, then as you have mentioned, it would become dependant on the layout used. If you use a null layout, then you can add the label at the drop location. If you want to draw the dragged image, then, you can use the repaint(x,y,w,h) method to signify where to draw and then draw the actual image in the paintComponent method.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Tim Sangster
Greenhorn

Joined: Mar 27, 2011
Posts: 12
Ranganathan Kaliyur Mannar wrote:Your question is contradictory. Initially, you mentioned about dragging a JPanel - then, you say you need something like that in UML tools.
I think what you want is to actually 'drop' something into a JPanel. So, in that case, you should propably have a JLabel or a JToggleButton with an image on it. Then, you would drag this component and drop it into the JPanel.

The examples given in the tutorial use StringSelection which is to transfer String data - so, in your case you should write a class that implements Transferable - this class should support DataFlavor.imageFlavor and the getTransferData method should return an Image object. Not an easy one to write though.

Finally, based on what to do on the 'drop' action - if you just want to add a JLabel to the panel, then as you have mentioned, it would become dependant on the layout used. If you use a null layout, then you can add the label at the drop location. If you want to draw the dragged image, then, you can use the repaint(x,y,w,h) method to signify where to draw and then draw the actual image in the paintComponent method.

Thank you for the response, You'r right I mean drop not only drag. I'm trying to work with a self written layout now and if that doesn't work out I will just draw everything myself.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1083
    
  10

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Drag and drop images