This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Drag and Drop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drag and Drop" Watch "Drag and Drop" New topic

Drag and Drop

Casey Buresh

Joined: Oct 03, 2006
Posts: 18
Hi my name is Casey, I'm currently taking AP Computer Science at my high school and I am working on a project that is worth quite a bit of my grade. My group leader decided that we would create a virtual closet. We've got most of the work done except for 1 part. I can't figure out how to use drag and drop. I don't understand it. Every site I look at gives me examples but never tells me how to use it, and when they do tell me how to use it, it only talks about dragging and dropping text. I need to drag and drop images so that the user and just move the image(the clothes) onto the manican in order to dress it up. That way they can position the clothing better on the manican. Any help would be greatly appreciated. Thanks.

'allo Poppet!!!
Casey Buresh

Joined: Oct 03, 2006
Posts: 18
No Answers??? Come on people please help me out here.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112

You are also more likely to get a good answer to this in the Swing forum. I'll move it for you...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
There a number of ways you can approach this.
If you are using separate components for the source images and drop target then you can take
advantage of the Swing built–in drag and drop system. This is explained with examples in the
tutorial page Introduction to Drag and Drop and Data Transfer. See the section
Data Transfer with a Custom Component. This is a general–purpose system and takes a
while to put together.
If you are doing all the dragging and dropping in a single component then things can be
greatly simplified. You have the option to use components or a graphics approach.
With components you mount the images in JLabels with ImageIcons, set the parent containers
layout to null, add the labels and call setBounds for each one. Use the setBounds
method to reposition the labels with your mouse code.
For the graphics approach you can use some Rectangles to both locate the images for drawing
and for detecting mouse selections (hit–testing) for dragging. I made up a small example of
this last option.

Image files are from the Examples Index, down low.
Casey Buresh

Joined: Oct 03, 2006
Posts: 18
Thanks that helps alot. Also today one of my friends in class (The Genius) gave me another idea. He talked about using MouseListener to simplify it by saying "If mouse clicks on picture then get mouse position" then a 'Triangle' would be drawn in coordination where the top left corner of the Image was located, and it would be locked to the mouse as long as it is held clicked. That way if the user wanted to save the "outfit" they had created then saving would be as easy as filling and empty two-dimensional array with the ImageIcon's name and where it was located (What coordinates) on the screen.
I agree. Here's the link:
subject: Drag and Drop
It's not a secret anymore!