Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Movable JPanel - How?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi D R,
you need a MouseMotionListener and use the mouseDragged function to capture the drage!
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you both for your help.

Darren
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic