aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Overlapping MouseListeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Overlapping MouseListeners" Watch "Overlapping MouseListeners" New topic
Author

Overlapping MouseListeners

Steven Hatfield
Greenhorn

Joined: Jan 25, 2010
Posts: 6
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

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Moving to our GUI forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jonas Wille
Greenhorn

Joined: Mar 29, 2010
Posts: 4
You can always calculate the position from the mouse for the backgroundcomponent.

Something like this could probably do the trick.


Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

Kevin, welcome to the Ranch, and thanks for your great advice
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4542
    
    5

Kevin Workman wrote:

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

Joined: Sep 28, 2010
Posts: 151
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

Joined: Jan 25, 2010
Posts: 6
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

Joined: Sep 28, 2010
Posts: 151
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overlapping MouseListeners