How to make mouse listeners independent from nested child components?

Hello,
the problem seems to be trivial. I just need mouse enter and exit events for Component which has inside many small child Components. By default it doesn’t work (or I do something wrong).
So to make it work I need for each child call in my parentComponent something like that:

childComponent01.addMouseListener(this, true);
childComponent02.addMouseListener(this, true);
childComponent03.addMouseListener(this, true);
//... and so on

And that would be OK, but the problem is my parentComponent::mouseExit() is called every time I exit from any child Component, even though mouse is still in the parent component bounds.

So I tried to check if:

parentComponent::mouseExit(const MouseEvent& e)
{
    if(!getLocalBounds().contains(e.getEventRelativeTo(this).getPosition()))
    {
        //something that I need to do on mouse exit
    }
}

And it almost work as I expect, but sometimes when I exit mouse with very slow move then if statement is not passed. So I am confused now because it looks like I don’t know how to make mouseExit() work for component which I wish and not dependent on child components.

For any help great thanks in advance.
Best Regards

Note that you don’t have to call addMouseListener() for every child. You can register yourself as a listener to all children with a single :

ParentComponent()
{
    addMouseListener (this, true);
}

Regarding the test, i’m not sure if it’s the issue you’re having, but your test would fail to do what you want (if I understood your need correctly) when the mouse goes from a child component to another.
You could try something like that instead:

ParentComponent::mouseExit (const MouseEvent&) override
{
    if (! isMouseOverOrDragging (true)
    {
    }
}

Hello lalala, thanks for your answer.
That’s fun I missed I can add mouse listener with one call of addMouseListener. For sure I will use it.

But the main problem is not I need to be notified when mouse enter/exit child Compinents. What I need is to be notified when mouse enter/exit my main Component independently of his child Components.

Ha ha ha, actually your suggestion works perfectly but only for mouseExit().

ParentComponent()
{
    addMouseListener (this, true);
}

And:

ParentComponent::mouseExit (const MouseEvent&) override
{
    if (! isMouseOverOrDragging (true)
    {
    }
}

It works perfectly for me. But… Man it’s killing me, ha ha ha. Now I have problems with mouseEnter().

With your code my parentComponent::mouseExit() is called only ones as I expect (I mean it goes only once inside if(! isMouseOverOrDragging (true))). But parentComponent::mouseEnter() is still called as many times as I move mouse on child components. And I can’t handle it.
I tried to call inside parentComponent::mouseEnter():

if (isMouseOverOrDragging (true))

But it is passed every time I enter to any child component, so actually it doesn’t make a difference.
So I tried:

if (isMouseOverOrDragging (false))

But in that case it is never passed.

Hmm… maybe here you have also any super duper advice?
Best regards and great thanks in advance.

not sure what you’re trying to achieve and if there’s a smarter way to do it, but you can perhaps just use a boolean:

ParentComponent()
{
    addMouseListener (this, true);
}


void mouseExit (const MouseEvent&) override
{
    if (! isMouseOverOrDragging (true)
    {
         isInside = false;
         ...
    }
}

void mouseEnter (const MouseEvent&) override
{
    if (! isInside && isMouseOverOrDragging (true)
    {
         isInside = true;
         ...
    }
}

bool isInside = false;

Great thanks lalala, that’s exactly what I want to achieve, but I was sure I can do that without any new bool variables. But that works for me so it’s perfect. Thanks.