The Red Penguin

Adding a slider listener


Wednesday 29 July 2020

The setup for this is very similar to adding the button - it goes in the same places in .h and .cpp but some of the syntax is different.

1. First step is to go into MainComponent.h. We need to add another inheritance relationship. So I have changed

class MainComponent : public juce::AudioAppComponent,
public juce::Button::Listener

to

class MainComponent : public juce::AudioAppComponent,
public juce::Button::Listener,
public juce::Slider::Listener

I've added juce:: to Slider::Listener - the video didn't mention it but looks like a feature of Juce 6.

2. Because I am now inheriting from Button::Listener I need to implement that function. So in the public section of that class (below void resized) I will enter:

    /** implement Slider::Listener */
void sliderValueChanged(juce::Slider *slider) override;


3. Next I need to go to the MainComponent.cpp file and implement it by adding code at the bottom:
void MainComponent::sliderValueChanged(juce::Slider* slider)
{
if (slider == &volSlider)
{
DBG("Vol slider moved to " << slider->getValue());
}
}


4. In the constructor MainComponent::MainComponent() in MainComponent.cpp where we configure the GUI, we can add more code to register the event:

volSlider.addListener(this);