Loading...
Searching...
No Matches
Tutorial: The Slider class

This tutorial introduces the Slider class, shows how to respond to slider movements, and how to obtain values from a slider. The tutorial also introduces some essential customisation techniques for displaying values with a slider.

Level: Beginner

Platforms: Windows , macOS , Linux , iOS , Android

Classes: Slider, Slider::Listener, Label

Getting started

Note
This tutorial leads on from Tutorial: Listeners and Broadcasters, which you should have read and understood first.

Download the demo project for this tutorial here: PIP | ZIP . Unzip the project and open the first header file in the Projucer.

If you need help with this step, see Tutorial: Projucer Part 1: Getting started with the Projucer.

The demo project

The demo project shows two linear horizontal sliders. One slider is labelled Frequency and the other is labelled Duration as shown in the following screenshot:

The demo project user interface showing two sliders and their values.

The idea is that both sliders essentially display the same underlying value since frequency (f ) is the reciprocal of duration (d ):

f = 1d

When either of the sliders are moved, the other one updates to reflect the change.

The JUCE Slider class.

This tutorial shows how to create the sliders, configure their range, listen for changes in value, and update the slider value programmatically. You will notice in the demo application, when it runs, that both sliders include a text box, and this text box also includes the units for frequency (Hz , Hertz) and duration (s , seconds).

Adding the sliders

The sliders have been added as private members to our MainContentComponent class:

private:
juce::Slider frequencySlider;
juce::Label frequencyLabel;
juce::Slider durationSlider;
juce::Label durationLabel;
//==============================================================================
};

Notice that we also add a Label object for each Slider object. These are to display the text Frequency and Duration to the left of the sliders. The boxes immediately to the left of the slider controls, which show the current slider values, are actually part of the Slider objects.

We have also added the Slider::Listener class as base class, so that we can register our class to receive slider changes

class MainContentComponent : public juce::Component,
public juce::Slider::Listener
{
public:

In our MainContentComponent constructor, we add the sliders as child components (see Tutorial: Parent and child components), make them visible, and configure the range of values that the slider can represent. First we configure the frequencySlider member:

MainContentComponent()
{
addAndMakeVisible (frequencySlider);
frequencySlider.setRange (50, 5000.0); // [1]
frequencySlider.setTextValueSuffix (" Hz"); // [2]
frequencySlider.addListener (this); // [3]
  • [1] : The range of the slider is set using the Slider::setRange() function.
  • [2] : We add a suffix to the text display in the slider's text box to show the value's units.
  • [3] : We add our MainContentComponent object as a listener to the slider.

The corresponding label is set up as follows:

addAndMakeVisible (frequencyLabel);
frequencyLabel.setText ("Frequency", juce::dontSendNotification);
frequencyLabel.attachToComponent (&frequencySlider, true); // [4]

The Label::attachToComponent() function [4] is really useful for placing a label adjacent to another component. The second argument, true , positions the label to the left of the other component (false would position it above). As we will see shortly, this avoids us having to position the labels manually in the MainContentComponent::resized() function.

The durationSlider and the durationLabel members are set up similarly, but the range of this slider is set to be the reciprocal of the range of the frequencySlider member:

addAndMakeVisible (durationSlider);
durationSlider.setRange (1.0 / frequencySlider.getMaximum(),
1.0 / frequencySlider.getMinimum());
durationSlider.setTextValueSuffix (" s");
durationSlider.addListener (this);
addAndMakeVisible (durationLabel);
durationLabel.setText ("Duration", juce::dontSendNotification);
durationLabel.attachToComponent (&durationSlider, true);

Positioning the slider

The sliders are positioned in the MainContentComponent::resized() function. Since we used the Label::attachToComponent() function to attach the labels to the sliders, these are positioned to the left of the sliders automatically.

void resized() override
{
auto sliderLeft = 120;
frequencySlider.setBounds (sliderLeft, 20, getWidth() - sliderLeft - 10, 20);
durationSlider .setBounds (sliderLeft, 50, getWidth() - sliderLeft - 10, 20);
}

Responding to slider changes

The following code makes the listeners of the sliders react to changes in the sliders' values.

void sliderValueChanged (juce::Slider* slider) override
{
if (slider == &frequencySlider)
durationSlider.setValue (1.0 / frequencySlider.getValue(), juce::dontSendNotification);
else if (slider == &durationSlider)
frequencySlider.setValue (1.0 / durationSlider.getValue(), juce::dontSendNotification);
}

This is the Slider::Listener::sliderValueChanged() function that we must override if we add Slider::Listener as a base class. Here we simply pass the reciprocal of the slider to the other slider by calling the Slider::setValue() function. We also tell the slider not to broadcast its change. This is because there is the potential for an infinite feedback loop to occur in cases such as this, where two sliders depend upon each other. The dontSendNotification value breaks this potential loop. Assuming the arithmetic is accurate, and the the conversions in both directions produce identical results, then this shouldn't be needed. This is because the slider will only broadcast to its listeners if the value has actually changed. (Problems can occur where there are slight rounding errors in the conversions in situations like this.) You can try omitting the dontSendNotification value which causes the default behaviour where the slider will broadcast changes. You really need to think carefully about whether to use dontSendNotification, or not, for specific use-cases in your own applications.

Setting the initial value

In the constructor the frequencySlider slider is set to a value of 500. This in turn will cause the durationSlider slider to update, since we omit the dontSendNotification value this time:

frequencySlider.setValue (500.0); // [5]

Some customisations

There are couple of simple customisations we can add here to make the interface more effective.

Making the text box wider

The text box for the durationSlider slider, in particular, needs many digits to display its value satisfactorily. To do this we can use the Slider::setTextBoxStyle() function. Add the following two lines of code to the MainContentComponent constructor:

frequencySlider.setTextBoxStyle (juce::Slider::TextBoxLeft, false, 160, frequencySlider.getTextBoxHeight());
durationSlider .setTextBoxStyle (juce::Slider::TextBoxLeft, false, 160, durationSlider .getTextBoxHeight());

This sets the text box to be 160 pixels in each case (but maintaining the height by using the Slider::getTextBoxHeight() function).

The sliders with wider text boxes.

Skewing the slider values

By default, the slider track is linear in the sense that the slider's value is proportional to the position of the slider thumb along the slider track. It is clear from manipulating the interface that this doesn't quite feel right. We can adjust the slider skew to make the slider track logarithmic. To do this we can use the Slider::setSkewFactorFromMidPoint() function. Try this out by adding the following two lines of code to the MainContentComponent constructor after the sliders have been configured:

frequencySlider.setSkewFactorFromMidPoint (500);
durationSlider .setSkewFactorFromMidPoint (0.002);

This places a value of 500 at the mid-point of the slider track for the frequencySlider slider, and 0.002 for the durationSlider slider. Effectively, the sliders will now seem to move equally but in opposite directions. A non-linear slider track like this works well for parameters such as time and frequency where we tend to want finer control over smaller values but need less fine control over larger values.

Note
The completed code for this section can be found in the SliderValuesTutorial_02.h file of the demo project for this tutorial.
Exercise
Try out different values for the calls to the Slider::setSkewFactorFromMidPoint() function and try out different text box sizes. Have a look at the API reference for the Slider class and try out some other customisations.

Simplifying the Slider callback

Instead of using the listeners and broadcasters paradigm as shown in this tutorial, we can simplify slider callbacks using lambda functions from the latest C++ standards. This works especially well for simple callbacks that don't require complex implementations.

First, let's remove the inheritance from the Slider::Listener class and restore the MainContentComponent class definition like this:

class MainContentComponent : public juce::Component
{
public:

Then, instead of adding the MainContentComponent as a listener to the Slider, assign a lambda function to the Slider::onValueChange helper object as follows:

MainContentComponent()
{
addAndMakeVisible (frequencySlider);
frequencySlider.setRange (50, 5000.0);
frequencySlider.setTextValueSuffix (" Hz");
frequencySlider.onValueChange = [this] { durationSlider.setValue (1.0 / frequencySlider.getValue(), juce::dontSendNotification); };
addAndMakeVisible (frequencyLabel);
frequencyLabel.setText ("Frequency", juce::dontSendNotification);
frequencyLabel.attachToComponent (&frequencySlider, true);
addAndMakeVisible (durationSlider);
durationSlider.setRange (1.0 / frequencySlider.getMaximum(),
1.0 / frequencySlider.getMinimum());
durationSlider.setTextValueSuffix (" s");
durationSlider.onValueChange = [this] { frequencySlider.setValue (1.0 / durationSlider.getValue(), juce::dontSendNotification); };

This tells the Slider object which function to call when the Slider value is changed by the user.

Note
The implementation of the code can be found in the SliderValuesTutorial_03.h file of the demo project for this tutorial.

Summary

In this tutorial we have introduced the Slider class. In particular we have learned:

  • How to set up a slider to operate over a specific range.
  • How to respond to slider value changes.
  • How to configure the slider skew to make it use a logarithmic scale.

See also

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram