Skip to main content

How to capture mouse events in a parent JPanel?

1 reply [Last post]
Joined: 2003-08-22

I'm sure this must be Swing 101, but I can't seem to find the equivalent of the standard Windows capture() function that redirects all mouse events to a particular control until it decides to release it.

basically, I don't want the mouseEntered or mouseExited events of the child controls to be triggered, I want the parent JPanel to always be notified when the mouse enters into it or one of its child controls.

Thanks in advance,

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-08-12

Add a MouseListener to all sub components in the JPanel. You can use the same MouseListener for them all.

An easy way is overriding .add(Component) in JPanel (and possibly other add methods) to do this automatically whenever a Component is added to the panel.