This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Movable JPanel - How? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Movable JPanel - How?" Watch "Movable JPanel - How?" New topic
Author

Movable JPanel - How?

D R Wilkinson
Ranch Hand

Joined: Oct 29, 2004
Posts: 51
Hello :-)

I need to make a JPanel within another ( null layout ) JPanel movable but I don't know how to aproach this.

I want the user to be able to click and hold on the movable JPanel, move the cursor ( with the JPanel following the cursor movements ) and then when the user releases the mouse click, the JPanel stays in place.

I know I can capture the required mouse events on the movable JPanel but I don't know how to get the JPanel to follow the cursor movements.

Can anyone help me please?

Thanks.
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
Hi D R,
you need a MouseMotionListener and use the mouseDragged function to capture the drage!
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You can use a JLayeredPane or a glassPane for this. The tutorial has a demonstration app for each on the page 'How to Use Root Panes'. See the JRootPane api for more about glassPanes. At the top of the (api) page there is a link into the tutorial to the root pane page.

For hand–made work an easy way is to mount a non–opaque JPanel over the parent panel with an OverlayLayout and use it for the mouse code. It will, of course, restrict access to the parent panel. But it has the advantage of covering only the parent panel, unlike a glassPane, leaving other ui components in the clear. One way to put this together:
D R Wilkinson
Ranch Hand

Joined: Oct 29, 2004
Posts: 51
thank you both for your help.

Darren
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Movable JPanel - How?