• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Overlapping MouseListeners

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

I need some help with getting 2 MouseListeners working at the same time on 2 different overlapping JPanels.
I have created a sample class that shows what is happening. Perhaps this is "the way that it is", but I need a way around the problem.

I have a background panel and a foreground panel. I need to track the mouse position in the background panel, even when it is over the foreground panel. The problem is that when the mouse is over the foreground panel, the background panel stops "listening".

Is there a way to keep both of the mouse listeners listening?

Thanks,
Steven

 
Sheriff
Posts: 22848
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our GUI forum.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can always calculate the position from the mouse for the backgroundcomponent.

Something like this could probably do the trick.


 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you could use a Glass Pane to detect the background MouseEvents instead.

Tutorial: How to Use Root Panes
Another article that's extremely helpful: A well-behaved GlassPane
 
Rob Spoor
Sheriff
Posts: 22848
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin, welcome to the Ranch, and thanks for your great advice
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin Workman wrote:


H Kevin, good to see you sharing your knowledge here too!
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:

Kevin Workman wrote:


H Kevin, good to see you sharing your knowledge here too!



Heh, thanks. Ever since the Sun Forums died, I've been looking a new home. It's nice to see a familiar face, er, name.
 
Steven Hatfield
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin Workman wrote:Or you could use a Glass Pane to detect the background MouseEvents instead.

Tutorial: How to Use Root Panes
Another article that's extremely helpful: A well-behaved GlassPane



This required a bit of work, and some luck, but I was able to get the GlassPane to work.

I used the sample GlassPane class from here: http://www.javafaq.nu/java-example-code-472.html
and changed it to accept the label that will be updated in my sample program. Now I get positioning within the blue box, the background, and overall positioning with the GlassPane. Fantastic!

Thank you!
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Hatfield wrote:
This required a bit of work, and some luck, but I was able to get the GlassPane to work.

I used the sample GlassPane class from here: http://www.javafaq.nu/java-example-code-472.html
and changed it to accept the label that will be updated in my sample program. Now I get positioning within the blue box, the background, and overall positioning with the GlassPane. Fantastic!

Thank you!



Cool, I'm glad you got it working. GlassPanes (and the other parts of a Root Pane) can be a bit tricky at first, but they start to make more sense as you use them more. Good luck!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic