Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overlapping MouseListeners

 
Steven Hatfield
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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

 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our GUI forum.
 
Jonas Wille
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.


 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin, welcome to the Ranch, and thanks for your great advice
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic