Can't Figure Out Why My mouseUp() Code Isn't Working For Me

I can’t figure out what I’m doing that is logically wrong, but I can’t get my mouseExit() code to work. I’m trying to have a function called whenever the mouse leaves a specific button’s boundaries. In the Projucer, the button’s name is “tapButton.” My mouseExit() code is:

void MainComponent::mouseExit (const MouseEvent& e)
    //[UserCode_mouseExit] -- Add your code here...

   if (e.eventComponent->getName() == tapButton->getName())


The if statement is reached, but it evaluates as false.

If I place:

std::cout << e.eventComponent->getName() << std::end;

inside the mouseExit() code and hover over the button and leave, it only prints whitespace and no actual button name, even though the button most certainly has a name associated with it.

My only guess is I’m not understanding which name is associated with what aspect of the component.

Here is the Projucer view of the button’s info:


Can anyone see my logical issue? Much appreciated.

Are you listening the button’s mouse events?

i.e, you should have that in your MainComponent constructor :

tapButton->addMouseListener (this, true);

ps: note that you could compare the pointers instead of the names:
if (e.eventComponent == tapButton)

Yes, this worked. Strangely enough, I never added the mouse listener, but what’s even more strange is that I originally was interacting with the button with mouseMove() and it was working even without a mouseListener, and I have no idea how that was working without a mouseListener. So when I switched over to mouseExit, I expected it to just work still. Any reason why mouseMove was able to interact with the button without registering a mouseListener?

And actually, it is throwing an error just trying to compare (e.eventComponent == tapButton)

I think because tapButton is a unique_pointer, we need:

(e.eventComponent == tapButton.get())

The Components are already MouseListeners. On every event, where e->eventComponent == this, the Component already reacts to the events. Only if you want code to be executed in a different object, that one can be registered as MouseListener to the original Component.

Actually, I know exactly why the button was still working when I was originally using mouseMove before even adding a mouseListner, another component was triggering mouseMove and the logic was was using was allowing it to call the function I needed to be called when it moved off of the button, but now it is all corrected and mouseExit works perfectly. I took about 10 months off of JUCE products and completely forgot to add the listener. Thanks again.

Hmmm, so why does a mouse listener need to be added when trying something to trigger mouseExit on a button that already is a mouseListener?

Because im trying to add additional functionality to the button on mousExit that is outside the scope of the button’s actual mouseExit code that is associated with it?

In your codeExample, you are implementing the mouseExit() in MainComponent. The tapButton is probably not a MainComponent, I assume by the name of it. The mouseExit happens on the tapButton though.

If the tapButton is a generic Component or Button, it is perfectly fine to use the Listener, just wanted to explain, why you had to do that. If your tapButton is already your own class, just move the implementation of mouseExit() there…

This makes total sense to me now. Thank you very much for the explanation.