# Changing Juce::Slider properties interrupts use of the slider

**URL:** <https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405>\
**Category:** General JUCE discussion\
**Created:** [March 16, 2023, 9:54pm UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405 "2023-03-16T21:54:02Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![loveslap](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@loveslap](https://forum.juce.com/u/loveslap)\
**Post date:** [March 24, 2023, 6:12pm UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/21 "2023-03-24T18:12:30Z")

</div>

I need to call paint?

---

<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:** [March 24, 2023, 7:09pm UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/22 "2023-03-24T19:09:20Z")

</div>

`repaint()` should already be called when the colour changes. There must have been something else happening in `lookAndFeelChanged()` that’s not happening any more. I’ll take a look. You should be able to call `sendLookAndFeelChange()` after setting the colour to reproduce the previous behaviour if you need to (i.e. it will interrupt the slider value).

---

<div class="post-metadata">

**Author:** ![loveslap](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@loveslap](https://forum.juce.com/u/loveslap)\
**Post date:** [March 24, 2023, 7:31pm UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/23 "2023-03-24T19:31:32Z")

</div>

So I really do need to use LookAndFeel regardless?

---

<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:** [March 24, 2023, 7:34pm UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/24 "2023-03-24T19:34:29Z")

</div>

Sorry I probably wasn’t clear enough. I’m just suggesting that’s what you can do as a temporary work around. I’ll take a look at this as soon as possible.

---

<div class="post-metadata">

**Author:** ![loveslap](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@loveslap](https://forum.juce.com/u/loveslap)\
**Post date:** [March 24, 2023, 7:38pm UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/25 "2023-03-24T19:38:20Z")

</div>

I need to write all the drawing routine for the slider?

---

<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:** [March 24, 2023, 7:52pm UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/26 "2023-03-24T19:52:35Z")

</div>

OK I think I know what’s going wrong, a fix will have to wait until next week though.

Thanks again for reporting.

---

<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:** [March 30, 2023, 7:51pm UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/27 "2023-03-30T19:51:32Z")

</div>

@loveslap sorry it’s taken so long to get back to this.

1. The changes I’ve made in develop will need to be reverted.
2. Although I agree the behaviour you’ve reported is certainly unexpected I will need to put this work on hold for a bit as the changes required to fix this are likely to have ramifications for many other users so a lot of thought and testing will need to go into the final solution.

All that being said @PaulDriessen already made a hint towards a good work around. Unfortunately it’s slightly more complicated than it may first appear as the colour you’re setting needs to be applied to a `Label` inside the `Slider` rather than the `Slider` itself.

Unfortunately the `Label` is a private and unexposed member of the `Slider` class however, we can be a little sneaky and get access to it via a custom LookAndFeel class. I’ve produced a working example below that demonstrates the technique.

```cpp
class SequencerSlider : public juce::Slider
{
public:
    SequencerSlider()
    {
        // do all the usual slider setup here
        setSliderStyle (juce::Slider::SliderStyle::LinearBarVertical);
        setRange (0.0, 10.0, 0.1);

        // calling this should trigger a call to our custom
        // createSliderTextBox() implementation
        setLookAndFeel (&lookAndFeel);

        // only call this after setting the look-and-feel
        setHighlighted (false);
    }

    ~SequencerSlider()
    {
        setLookAndFeel (nullptr);
    }

    // by overriding this we prevent the base class getting the call
    // if it did it would end up calling createSliderTextBox which we
    // don't want as that's the cause of the interruptions to the
    // sliders mouse events!
    void colourChanged() override
    {
        // this allows the look-and-feel to update the labels colours
        lookAndFeel.sliderColourChanged (*this);

        // we'll also need to tell this component to repaint now a
        // colour has changed
        repaint();
    }

    void setHighlighted (bool shouldHighlight)
    {
        setColour (juce::Slider::textBoxOutlineColourId, shouldHighlight ? juce::Colours::white
                                                                         : juce::Colours::black);
    }

private:
    // make sure to inherit from the actual look-and-feel class
    // we want to use for the rest of the slider drawing
    class LookAndFeel : public juce::LookAndFeel_V4
    {
    public:
        juce::Label* createSliderTextBox (juce::Slider& slider)
        {
            // we pass this call on to the actual look-and-feel we want
            // to use, but before returning it to the underlying slider
            // we store the value in a member variable for later use (sneaky!)
            auto* l = juce::LookAndFeel_V4::createSliderTextBox (slider);
            label = l;

            // we call this to make sure we've setup all the label
            // colours correctly
            sliderColourChanged (slider);

            return l;
        }

        void sliderColourChanged (juce::Slider& slider)
        {
            if (label == nullptr)
                return;

            // This is code effectively taken from juce::LookAndFeel_V2::createSliderTextBox()
            // it is the colours that will be used for a LinearBar or LinearBarVertical slider
            // in almost all cases, feel free to change them if needed, but generally using
            // setColour on the slider should be sufficient
            label->setColour (Label::textColourId, slider.findColour (Slider::textBoxTextColourId));
            label->setColour (Label::backgroundColourId, Colours::transparentBlack);
            label->setColour (Label::outlineColourId, slider.findColour (Slider::textBoxOutlineColourId));

            label->setColour (TextEditor::textColourId, slider.findColour (Slider::textBoxTextColourId));
            label->setColour (TextEditor::backgroundColourId, slider.findColour (Slider::textBoxBackgroundColourId).withAlpha (0.7f));
            label->setColour (TextEditor::outlineColourId, slider.findColour (Slider::textBoxOutlineColourId));
            label->setColour (TextEditor::highlightColourId, slider.findColour (Slider::textBoxHighlightColourId));
        }

    private:
        // we use a weak reference here so if the label is deleted
        // from under our feet we wont accidentally dereference it!
        juce::WeakReference<Component> label;
    };

    // This gives us a unique instance of our look-and-feel per slider
    // meaning each instance can store its own reference to its label
    LookAndFeel lookAndFeel;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SequencerSlider)
};

// Code below here is just an example of how we might use the above slider in practice
//==============================================================================
class MyComponent : public juce::Component
                   , private juce::Timer
{
public:
    //==============================================================================
    MyComponent()
    {
        createAndAddSliders (8);
        highlightSlider (0);
        setSize (600, 400);
        startTimerHz (1);
    }

    ~MyComponent() override
    {
        stopTimer();
    }

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

    void resized() override
    {
        auto bounds = getLocalBounds();
        auto sliderSize = bounds.getWidth() / sliders.size();

        for (auto* slider : sliders)
            slider->setBounds (bounds.removeFromLeft (sliderSize));
    }

private:
    void createAndAddSlider()
    {
        auto slider = std::make_unique<SequencerSlider>();
        addAndMakeVisible (slider.get());
        sliders.add (std::move (slider));
    }

    void createAndAddSliders (int numSliders)
    {
        for (int i = 0; i < numSliders; ++i)
            createAndAddSlider();
    }

    void highlightSlider (int index)
    {
        sliders[highlightedSliderIndex]->setHighlighted (false);

        highlightedSliderIndex = index % sliders.size();
        sliders[highlightedSliderIndex]->setHighlighted (true);
    }

    void timerCallback() override
    {
        highlightSlider (highlightedSliderIndex + 1);
    }

    //==============================================================================
    juce::OwnedArray<SequencerSlider> sliders;
    int highlightedSliderIndex {};

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MyComponent)
};

```

I hope that helps.

All the best, and thanks again for reporting.

---

<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:** [March 31, 2023, 9:14am UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/28 "2023-03-31T09:14:41Z")

</div>

It dawned on me this morning that there is an easier way to get the underlying label, just loop through the children to find it instead.

```cpp
class SequencerSlider : public juce::Slider
{
public:
    SequencerSlider()
    {
        // do all the usual slider setup here
        setSliderStyle (juce::Slider::SliderStyle::LinearBarVertical);
        setRange (0.0, 10.0, 0.1);
        setHighlighted (false);
    }

    // by overriding this we prevent the base class getting the call
    // if it did it would end up calling createSliderTextBox which we
    // don't want as that's the cause of the interruptions to the
    // sliders mouse events!
    void colourChanged() override
    {
        // this allows us to update the labels colours using the
        // colours from the slider
        updateLabelColours();

        // we should also tell this component to repaint as one of its
        // colours may have changed
        repaint();
    }

    void setHighlighted (bool shouldHighlight)
    {
        setColour (juce::Slider::textBoxOutlineColourId, shouldHighlight ? juce::Colours::white
                                                                         : juce::Colours::black);
    }

private:
    Label* findLabel()
    {
        // by searching through all the children and returning the
        // first child of type `Label` it should give us the label
        // component we need in order to update it's colours without
        // completely recreating it
        for (auto* child : getChildren())
        {
            if (auto* l = dynamic_cast<Label*> (child))
                return l;
        }

        return nullptr;
    }

    // this will be called whenever a child component is added or
    // removed, giving us an opportunity to get a pointer to the label
    // we want to set the colours on once it's been added
    void childrenChanged() override
    {
        label = findLabel();
        updateLabelColours();
    }

    void updateLabelColours()
    {
        if (label == nullptr)
            return;

        // This is code effectively taken from juce::LookAndFeel_V2::createSliderTextBox()
        // it is the colours that will be used for a LinearBar or LinearBarVertical slider
        // in almost all cases, feel free to change them if needed, but generally using
        // setColour on the slider should be sufficient
        label->setColour (Label::textColourId, findColour (Slider::textBoxTextColourId));
        label->setColour (Label::backgroundColourId, Colours::transparentBlack);
        label->setColour (Label::outlineColourId, findColour (Slider::textBoxOutlineColourId));

        label->setColour (TextEditor::textColourId, findColour (Slider::textBoxTextColourId));
        label->setColour (TextEditor::backgroundColourId, findColour (Slider::textBoxBackgroundColourId).withAlpha (0.7f));
        label->setColour (TextEditor::outlineColourId, findColour (Slider::textBoxOutlineColourId));
        label->setColour (TextEditor::highlightColourId, findColour (Slider::textBoxHighlightColourId));
    }

    Label* label {nullptr};

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SequencerSlider)
};

```

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [March 31, 2023, 10:01am UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/29 "2023-03-31T10:01:46Z")

</div>

Nice idea.  
I think it can be improved further: there is no need to keep the label pointer, removing any chance to get a dangling pointer.  
This is all you need:

```auto
    // this will be called whenever a child component is added or
    // removed
    void childrenChanged() override
    {
        updateLabelColours();
    }

    void updateLabelColours()
    {
        if (auto* label =findLabel())
        {

            // This is code effectively taken from juce::LookAndFeel_V2::createSliderTextBox()
            // it is the colours that will be used for a LinearBar or LinearBarVertical slider
            // in almost all cases, feel free to change them if needed, but generally using
            // setColour on the slider should be sufficient
            label->setColour (Label::textColourId, findColour (Slider::textBoxTextColourId));
            label->setColour (Label::backgroundColourId, Colours::transparentBlack);
            label->setColour (Label::outlineColourId, findColour (Slider::textBoxOutlineColourId));

            label->setColour (TextEditor::textColourId, findColour (Slider::textBoxTextColourId));
            label->setColour (TextEditor::backgroundColourId, findColour (Slider::textBoxBackgroundColourId).withAlpha (0.7f));
            label->setColour (TextEditor::outlineColourId, findColour (Slider::textBoxOutlineColourId));
            label->setColour (TextEditor::highlightColourId, findColour (Slider::textBoxHighlightColourId));
        }
    }

```

But if you want to keep it, I would recommend using a  
`juce::Component::SafePointer<juce::Label> label;`

---

<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:** [March 31, 2023, 10:03am UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/30 "2023-03-31T10:03:56Z")

</div>

Nice touch!

---

<div class="post-metadata">

**Author:** ![loveslap](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@loveslap](https://forum.juce.com/u/loveslap)\
**Post date:** [July 10, 2023, 9:25pm UTC](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405/31 "2023-07-10T21:25:00Z")

</div>

This works quite well for my but one problem is that click on the slider makes a cursor appear.  
How can I surpress the cursor?

[Previous page](https://forum.juce.com/t/changing-juce-slider-properties-interrupts-use-of-the-slider/55405.md?page=1)
