paintButton() is called permanently

Hello guys,

I created my own button and when I push the button it’s method paintButton() is called permanently.

The code looks as follows:

class MyButton :  public juce::Button
{
public:
    MyButton(const juce::String& buttonName = {}) : juce::Button(buttonName)
    {
    };
    void paintButton(Graphics& g, bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown) override
    {
    };
};

I added an instance of MyButton to gui. When I move the mouse over the button paintButton() is called a single time. The same when I move the mouse away from the button. This is the expected behavior. So far so good.

If I press the button, paintButton() is called permanently. Even if I turn off the button, the method is still called. Why is this so and is this behavior normal? I would expect paintButton() to be called only once.

I would appreciate an explanation. Thanks in advance!

So, a few questions. When you say ‘permanently’ do you mean ‘constantly’? Is there an issue with paint being called often? certainly there could be a bug you are experience (if so, likely your own code), but is there an issue you are experiencing, or you are just curious?

Also, when you say you ‘turn the button off’, what do you mean? if the button is visible, it will need to be repainted when something happens to trigger repainting.

paintButton() is called from the Component paint(). This is a callback, which should only paint and have no side effects.
Your code needs to be ok with paintButton() being called at any time, because there are plenty of reasons, why the OS decides the button needs to be repainted.

For your particular case, I guess the mouse events call a paint each time, so your component can react to changes issued by the mouse drag. But that’s something you could find out with the debugger. However like said before, there is little you can do against it, just write your code so it doesn’t matter.

Thanks for your response.

When you say ‘permanently’ do you mean ‘constantly’?

Yes, I mean ‘constantly’.

Is there an issue with paint being called often? certainly there could be a bug you are experience (if so, likely your own code), but is there an issue you are experiencing, or you are just curious?

No, there are no issues. And yes, I’m just curious because constantly calling paintButton() does not seem very efficient to me.

For your particular case, I guess the mouse events call a paint each time

No, I ensured that the mouse is not moved. It’s interesting that mouseEnter() and mouseExit() trigger paintButton() only once when the button is off (and the mouse is not moved). On the other hand paintButton() is called constantly when the button is down (and the mouse is not moved).

For your information: I tested this using Windows 10. I don’t know if macOS reacts in the same way.