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