Component movement???

A

Arinté

How can code moving components around with the mouse that are inside a
JPanel. Basically I want it to be a little like drag and drop. Will I have
to use java.awt.dnd.* or is there a way to do it playing around with
listeners and focus?

thanx
 
C

Chris Smith

Arinté said:
How can code moving components around with the mouse that are inside a
JPanel. Basically I want it to be a little like drag and drop. Will I have
to use java.awt.dnd.* or is there a way to do it playing around with
listeners and focus?

Generally speaking, the solution I'd look into is using wrapper
components to present the components you want to move, but intercept any
user interaction. The paint method of these wrapper components would
delegate to the original components that they wrap.

You wouldn't want to use the drag and drop functionality of the Java
API, because the items you're dragging and dropping don't represent data
being transferred from one component to another. The API for drag and
drop is designed for that data transfer situation.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Jesper Nordenberg

Arinté said:
How can code moving components around with the mouse that are inside a
JPanel. Basically I want it to be a little like drag and drop. Will I have
to use java.awt.dnd.* or is there a way to do it playing around with
listeners and focus?

Just put a MouseMotionListener on the component you want to drag. Then
listen for mouseReleased() in a MouseListener on the same component.
As long as the mouse button is pressed (during the drag) the "dragged"
component will get the mouseReleased() event. If you want to change
the mouse cursor when dragging use setCursor() on the JPanel (remember
to set it back :). You can find the standard DnD cursors as constants
in java.awt.dnd.DragSource.

I recently created a docking window framework (similar to that in
Visual Studio 7) using this technique. To draw the outline of the
dragged windows I created a JPanel at the top which overloaded the
paint() method and painted a rectangle on top of all child components.
When the rectangle moved I just called repaint(Rectangle) on the
panel.

Hope this helps!

/Jesper Nordenberg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top