Child component doesn't receive MouseEvents

I’m a bit stumped. I have a component containing another child component. In the child component, I have overrides for mouseEnter, mouseExit etc. - but none of them hit! I thought since it is a component class, it would automatically receive these if implemented.

I have tried, in the parent component, all variations of setInterceptsMouseClicks() to no avail. What simple thing am I missing?

This is a simple and basic thing, but it is not easy to know what you are doing wrong from your description.

Sorry. I want to receive mouseEvents in my child component when the mouse is over it, but not receive them when it is over the rest of the parent component.

Simply implementing the mouseEnter, mouseExit overrides in the child component does nothing by itself. They are not called.

Since I posted this, I found that if I add the child component as a MouseListener in the parent component, the mouse functions will be called in the child component. However, they will be called NOT only when it is over the child component, but when it is over any part of the parent component as well.

in parent component:
addMouseListener(&child, true);

I tried implementing hitTest() in the parent component to only recognize the area of the child component, which sort of works - but then other child components in the parent are excluded.

Don’t know if that made sense…

Hmm I think something else is happening as what you’re describing should be the default behavior.

Are you sure there’s no invisible corner of another component sitting of your child or anything of that sort?

Yeah, there’s nothing touching it or overlapping it. So you’re saying the normal behavior should just be to add those mouse function overrides to the child component and they should be called, without having to add a MouseListener?

Yeah typically the mouse events goes to the component it’s over regardless of the parent hierarchy.

You would add mouseListener or something like getMouseEvents for children to get the mouse events in the parent but not the other way around.

Have you done a sanity check in a vanilla app maybe?

That’ll be my next step…

EDIT: yeah, you’re right, it does work that way normally. As I thought…

So something in my app’s code is interfering…

How big are your child components? I’ve noticed recently in the plugin I’m working on that on some very small components (about 10 pixels square) the area they react to mouse events is off. I haven’t had a chance to look into why, but it’s quite annoying that the mouse can appear to be over the component but they do not register as such, the area they do react to is up and to the left of what is being painted on screen (no setPaintingIsUnclipped on them, so it’s not that).

Thanks for the suggestion. But they are rather bigger than that. Actually, I just figured out this mess, due mainly to my inexperience.

My child component (being discussed) actually has children of its own, two buttons that occupy the entire area, so THEY are grabbing/intercepting all mouse events. The child is essentially a wrapper for the two buttons.

Parent → child → [2 buttons]

I wanted to get mouse events in the child/wrapper, but they are being intercepted by the buttons and there seems to be no setting of setInterceptsMouseClicks() that allows a way for the child/wrapper to get the events as well as the buttons.

So it seems the way is to add a MouseListener to the button, with a pointer to the child, i.e.

button->addMouseListener(child, true);

Now it works…

1 Like