# Component setOpaque and setAlpha

**URL:** <https://forum.juce.com/t/component-setopaque-and-setalpha/51389>\
**Category:** General JUCE discussion\
**Created:** [May 10, 2022, 6:33pm UTC](https://forum.juce.com/t/component-setopaque-and-setalpha/51389 "2022-05-10T18:33:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RolandMR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rolandmr/32/641_2.png) [@RolandMR](https://forum.juce.com/u/RolandMR)\
**Post date:** [May 10, 2022, 6:33pm UTC](https://forum.juce.com/t/component-setopaque-and-setalpha/51389/1 "2022-05-10T18:33:32Z")

</div>

Painting doesn’t work correctly if `setOpaque(true)` and a parent component has `setAlpha (0.5f)` set. There is a check here:

```auto
if (child.isOpaque() && child.componentTransparency == 0)
{
    g.excludeClipRegion (newClip + delta);
    wasClipped = true;
}

```

But this test is not enough, you can’t just test if the component has an alpha of 0, you need to go up the component hierarchy and make sure all the parents are alpha 0 as well.

I believe it should be fixed like this: [reFX: Fix drawing of opaque components · reFX/JUCE@6302f18 · GitHub](https://github.com/reFX/JUCE/commit/6302f185534370f6666bbe1bf13bfa814f4b01ad)

---

<div class="post-metadata">

**Author:** ![unlinked1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/unlinked1/32/10039_2.png) [@unlinked1](https://forum.juce.com/u/unlinked1)\
**Post date:** [June 14, 2023, 11:29am UTC](https://forum.juce.com/t/component-setopaque-and-setalpha/51389/2 "2023-06-14T11:29:47Z")

</div>

I think I have a simple repro where you can see the effects of this - it’s an inner frame with `setOpaque (true)` inside an outer frame with both `setOpaque (false)` and `setAlpha` \< 1.0f.

Toggling the outer frame opacity will often result in cached artifacts, e.g:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/a/a6f43b6ded575e2aeb8d719a39027f262a0b32c6.png)

Here’s the code:

```auto
#include <juce_gui_extra/juce_gui_extra.h>
#include <functional>

class MainComponent : public juce::Component
{
public:
    MainComponent()
    {
        setSize (200, 300);

        addAndMakeVisible (&outer);
        outer.setColour (juce::Colours::white);
        outer.setOpaque (false); // we're going to toggle the alpha of this

        outer.addAndMakeVisible (&inner);
        inner.setColour (juce::Colours::yellow);
        inner.setOpaque (true); // this will always be full alpha

        addAndMakeVisible (&button);
        button.setButtonText ("press");

        mouseListener.listenTo (button);
        mouseListener.setMouseDown (
            [this]()
            {
                float newAlpha = outer.getAlpha() == 1.0f ? 0.5f : 1.0f;
                outer.setAlpha (newAlpha);
            });
    }

    void paint (juce::Graphics& g) override
    {
        g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));
    }

    void resized() override
    {
        outer.setBounds ({50, 50, 100, 100});
        inner.setBounds ({25, 0, 50, 100});
        button.setBounds ({50, 200, 100, 50});
    }

private:
    //==============================================================================
    class Block : public juce::Component
    {
    public:
        void setColour (juce::Colour c) { colour = c; }

    private:
        void paint (juce::Graphics& g) override { g.fillAll (colour); }
        juce::Colour colour;
    };

    class MouseListener : public juce::MouseListener
    {
    public:
        MouseListener() {}
        void listenTo (juce::Component& c) { c.addMouseListener (this, false); }
        void setMouseDown (std::function<void()> onDown) { onMouseDown = onDown; }
        void mouseDown (const juce::MouseEvent&) override { onMouseDown(); }

    private:
        std::function<void()> onMouseDown;
    };

    Block outer;
    Block inner;
    juce::TextButton button;
    MouseListener mouseListener;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

```

In practice this means you can’t use `setOpaque (true)` on any component if you can’t be sure the entire hierarchy is also opaque. Is this correct?

---

<div class="post-metadata">

**Author:** ![RolandMR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rolandmr/32/641_2.png) [@RolandMR](https://forum.juce.com/u/RolandMR)\
**Post date:** [June 22, 2023, 10:02pm UTC](https://forum.juce.com/t/component-setopaque-and-setalpha/51389/3 "2023-06-22T22:02:56Z")

</div>

Here is the latest fix for the issue: [reFX: Fix drawing of opaque components · reFX/JUCE@6302f18 · GitHub](https://github.com/reFX/JUCE/commit/6302f185534370f6666bbe1bf13bfa814f4b01ad)

Changes to juce broke my previous fix. Any chance of getting this integrated into mainline juce?

---

<div class="post-metadata">

**Author:** ![unlinked1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/unlinked1/32/10039_2.png) [@unlinked1](https://forum.juce.com/u/unlinked1)\
**Post date:** [June 30, 2023, 9:33am UTC](https://forum.juce.com/t/component-setopaque-and-setalpha/51389/4 "2023-06-30T09:33:12Z")

</div>

Any chance of someone from the JUCE team taking a look and advising whether they can repro and if so, if it’s something they consider worth fixing?

It would help to know to if we need to avoid these situations totally.

Thanks!

---

<div class="post-metadata">

**Author:** ![unlinked1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/unlinked1/32/10039_2.png) [@unlinked1](https://forum.juce.com/u/unlinked1)\
**Post date:** [July 25, 2023, 5:56pm UTC](https://forum.juce.com/t/component-setopaque-and-setalpha/51389/5 "2023-07-25T17:56:33Z")

</div>

Any chance of getting any kind of feedback from the JUCE team on this, even if it’s just to say it’s not a priority to fix?

Thanks,

Steve.

---

<div class="post-metadata">

**Author:** ![reuk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/reuk/32/21494_2.png) [@reuk](https://forum.juce.com/u/reuk)\
**Post date:** [July 26, 2023, 9:45am UTC](https://forum.juce.com/t/component-setopaque-and-setalpha/51389/6 "2023-07-26T09:45:24Z")

</div>

This is on our radar, but there won’t be any related changes in the next point release.

---

<div class="post-metadata">

**Author:** ![unlinked1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/unlinked1/32/10039_2.png) [@unlinked1](https://forum.juce.com/u/unlinked1)\
**Post date:** [July 26, 2023, 10:01am UTC](https://forum.juce.com/t/component-setopaque-and-setalpha/51389/7 "2023-07-26T10:01:06Z")

</div>

Thanks for the response Reuben. When it bubbles up, let me know if there’s anything I can do to help.

---

<div class="post-metadata">

**Author:** ![RolandMR](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rolandmr/32/641_2.png) [@RolandMR](https://forum.juce.com/u/RolandMR)\
**Post date:** [November 14, 2025, 5:42pm UTC](https://forum.juce.com/t/component-setopaque-and-setalpha/51389/8 "2025-11-14T17:42:06Z")

</div>

@anthony-nicholls would you expect this to be fixed in [Component: Improve opaque component checks · juce-framework/JUCE@961ff32 · GitHub](https://github.com/juce-framework/JUCE/commit/961ff32b9e299becb8f580facbf75f7d8dd6408d) ?

I just had a merge conflict updating my juce branch, so I removed my changes. I tested the program by @unlinked1 and I’m not seeing the issue.

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [November 14, 2025, 5:47pm UTC](https://forum.juce.com/t/component-setopaque-and-setalpha/51389/9 "2025-11-14T17:47:54Z")

</div>

Yes I came across this issue while doing the changes, I think there is a unit test in Component that captures this now too.
