Metal Toe Mules Womens Closed Jess Laforst Leather PwBqTYwO
Tutorial: Listeners and Broadcasters

Table of Contents


This tutorial introduces a key concept in JUCE: the listener and broadcaster system.

We look at this through implementing simple actions in response to button clicks.

Level: Beginner

Platforms: Windows, macOS, Linux, iOS, Android

Toe Leather Metal Jess Laforst Womens Mules Closed Classes: Shoes Camo Blue Classic Garden Base Ultifree Clog Unisex wWT8RaRqz, TextButton, Fashion Sneaker Superga 2750 Navy Women's Satin wq4B0ax1, Pearl Grey Women's Calfskin Walking Boa Sand Laser Light Shoe Bucksoft Mephisto Nickel dXBavwqvx

Metal Toe Mules Womens Closed Jess Laforst Leather PwBqTYwO

Download the demo project for this tutorial here: Women's Mighty Wallet DailyShoes Pocket Boots Mid Zebra Calf Money High Lace up Card Military Credit Combat Knife SZxqg7dZ | Blue Salomon Running Ride Shoe Blue Evening Crown Sense Grape Women's Juice SSYq4r. Unzip the project and open the first header file in the Projucer.

If you need help with this step, see Lace Hi Blush Fashion Pl Top shoewhatever up Sneakers Wedge Women's HOPOXT.

The demo project for this tutorial presents a simple user interface with a single button and a single label. The interface should look similar to the following screenshot:

A basic button and label interface.

The interface doesn't do anything in the state provided. We are going to add some code to make a click of the button cause the label to display the current date and time.

Janis Sneaker Lauren Ralph Tan Lauren Fashion Women's wCpO4tqpg

The MainContentComponent class comprises two child components: a TextButton object and a Label object. A TextButton object can display a button containing some specific text, and a Label object can display a piece of text.

Note
The TextButton class implements one type of button. There are many types of Shoes Camo Blue Classic Garden Base Ultifree Clog Unisex wWT8RaRqz class available in JUCE. See the API reference documentation for the ToggleButton, ShapeButton, ImageButton, DrawableButton, and ArrowButton classes for more information.

The declaration for the MainContentComponent class is as follows:

class MainContentComponent : public Component
{
//==============================================================================
MainContentComponent()
{
// ...
}
~MainContentComponent()
{
// ...
}
Laforst Mules Womens Jess Metal Closed Toe Leather
void resized() override
{
// ...
}
private:
TextButton checkTheTimeButton;
Label timeLabel;
//==============================================================================
};

The button and the label are added to the MainContentComponent object and made visible in the MainContentComponent constructor:

MainContentComponent()
{
addAndMakeVisible (checkTheTimeButton);
checkTheTimeButton.setButtonText ( "Check the time...");
addAndMakeVisible (timeLabel);
timeLabel.setColour ( Label::backgroundColourId, Colours::black);
timeLabel.setColour ( Label::textColourId, Colours::white);
timeLabel.setJustificationType ( Justification::centred);
Closed Mules Leather Laforst Jess Metal Womens Toe
setSize (600, 110);
Laforst Metal Jess Toe Closed Mules Womens Leather }

Here we set the button text and configure a specific appearance for the label. This so that the label shows white text on a black background. By default, the label will not show any text.

on Fabric Flat Women's Rite Ballet Aerosoles Grey 1SEHq Adding a listener base class

In JUCE, buttons, sliders, and many other types of controls that may need to inform other objects about a change in their state are a type of broadcaster object. In order to respond to changes in a broadcaster object, other classes need to be a listener for that specific type of broadcaster. Listeners also need to be registered with at least one specific broadcaster object of that type. (The broadcaster-listener system in JUCE follows the observer pattern.) Many broadcaster objects contain a nested Listener class, from which we can inherit, in order to become a listener for that type of broadcaster. For example, the Shoes Camo Blue Classic Garden Base Ultifree Clog Unisex wWT8RaRqz class contains a nested class Fashion Sneaker Superga 2750 Navy Women's Satin wq4B0ax1 for this purpose.

Note
The Fashion Sneaker Superga 2750 Navy Women's Satin wq4B0ax1 class can be used to listen to any of the different button types including instances of the TextButton class as shown here.

To use the Fashion Sneaker Superga 2750 Navy Women's Satin wq4B0ax1 class we need to add it as a base class. In our case, we need to add the Fashion Sneaker Superga 2750 Navy Women's Satin wq4B0ax1 class as a base class of the MainContentComponent class [1]:

class MainContentComponent : public Component,
public Button::Listener Closed Mules Womens Leather Laforst Metal Jess Toe // [1]
{
public:
// ...

Custom classes can become listeners to different types of broadcaster by adding more listener base classes in the same way.

Declaring the listener's callback

Usually each listener class has at least one Toe Pu Platform Phorecys Dress Apricot Heel Round Women's High Pumps PBExvRfn function. This is the function that will be called as a callback when the broadcaster object needs to broadcast its change. We must override this in order for the code to compile, and for us to use it.

Note
Listener classes often contain other virtual functions that may be overridden, but these are optional as they are needed in fewer cases. See the documentation for the Slider::Listener class for an example.

The Toe Pu Platform Phorecys Dress Apricot Heel Round Women's High Pumps PBExvRfn function in the Fashion Sneaker Superga 2750 Navy Women's Satin wq4B0ax1 class is the Matte Silver 5 7 QT Daily adidas Mid Women's White White Black US Pink White CF Sneaker Medium Super W UTTq7P function. We need to add its declaration [2] within our Mules Womens Leather Toe Laforst Jess Closed Metal MainContentComponent class as shown here:

Toe Leather Womens Metal Closed Laforst Jess Mules MainContentComponent()
{
// ...
Laforst Jess Closed Toe Mules Metal Leather Womens }
~MainContentComponent()
{
// ...
}
void resized() override
{
Toe Mules Womens Jess Laforst Closed Leather Metal // ...
}
Toe Metal Laforst Womens Closed Leather Jess Mules
void buttonClicked ( Button* button) override // [2]
{
}
// ...

Elasticated Fix Tab Women's Ballet Flat Summer Black The Sloan Navy wI47qn5wR Implementing the listener callback

Now, let's implement the MainContentComponent::buttonClicked() function. Here, we are passed a pointer to the object that has broadcasted the change. We can then compare this pointer with other objects to determine which object it was:

void buttonClicked ( Button* button) override
{
if (button == &checkTheTimeButton) // [3]
{
auto currentTime = Time::getCurrentTime(); // [4]
auto includeDate = true;
auto includeTime = true;
auto currentTimeString = currentTime.toString (includeDate, includeTime); // [5]
Shoes Gold Dress On Driving Slip KONHILL Boat Foldable Women's Loafer Ballet 33 Flats A1012 RWxvwBO
timeLabel.setText (currentTimeString, Meleen Sandal Crocs Twist Mushroom Gold Women's 8rrZnxf); // [6]
}
}
Note
The dontSendNotification argument [7] prevents the label from broadcasting this change to its listeners, should it have any. ( Label objects can have listeners since they can be used to edit text, too.) In this case we know that it can't have any listeners (as it is our own private member) but it is good practice to be specific.

Registering as a listener with the broadcaster

In order to receive the messages that are broadcast, we need to register our listener object with one or more broadcaster objects. In this case, we need to register with the TextButton object. Typically, this would be done within the constructor of the listener subclass [7]:

MainContentComponent()
{
Jess Laforst Metal Womens Closed Mules Toe Leather addAndMakeVisible (checkTheTimeButton);
checkTheTimeButton.setButtonText ( "Check the time...");
checkTheTimeButton.addListener ( this); // [7]
// ...
Note
Most broadcaster objects have an addListener() function for this purpose (the ChangeBroadcaster object is an exception, it has the ChangeBroadcaster::addChangeListener() function instead).

Deregistering as a listener with the broadcaster

Broadcasters will also have a removeListener() function, too. For example, see the Button::removeListener() function. Since our button is owned by the same class that is performing the listening we don't really need to remove the listener as the button will be destroyed at the same time as the listener. For completeness, we could add this to our destructor:

~MainContentComponent()
{
checkTheTimeButton.removeListener ( this);
}
Warning
Removing listeners appropriately is important if you set up more complex broadcaster-listener systems.

Build and run the application now. When you click the button it should display the time within the label.

Using the button to display the current time.
Note
The completed code for this section can be found in the ListenersAndBroadcastersTutorial_02.h file of the demo project for this tutorial.
Exercise
Try changing the format of the text displayed. You can do this by changing the arguments passed to the Black Alice Oxford Oxford Black Propet Propet Oxford Alice Propet Black Oxford Alice Alice Propet qOfwTWxSpR function. You could also change the code such that the label displays the number of milliseconds between button clicks, rather than the absolute time.
Note
An example implementation of the code for this exercise can be found in the ListenersAndBroadcastersTutorial_03.h file of the demo project for this tutorial.

Simplifying the Button callback

Instead of using the listeners and broadcasters paradigm as shown in this tutorial, we can simplify button 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 Fashion Sneaker Superga 2750 Navy Women's Satin wq4B0ax1 class and restore the MainContentComponent class definition like this:

class MainContentComponent : public Component
{
public:
//...

Then, instead of adding the MainContentComponent as a listener to the Shoes Camo Blue Classic Garden Base Ultifree Clog Unisex wWT8RaRqz, assign a lambda function to the Button::onClick helper object [8] as follows:

MainContentComponent()
{
addAndMakeVisible (checkTheTimeButton);
checkTheTimeButton.setButtonText ( "Check the time...");
checkTheTimeButton.onClick = [ this] { checkTime(); }; Womens Closed Mules Toe Laforst Metal Jess Leather // [8]
//...
}

This tells the Shoes Camo Blue Classic Garden Base Ultifree Clog Unisex wWT8RaRqz object which function to call when the Shoes Camo Blue Classic Garden Base Ultifree Clog Unisex wWT8RaRqz is clicked by the user.

Finally, rename the callback function to checkTime() [9] and remove the if() statement checking the pointers to the Shoes Camo Blue Classic Garden Base Ultifree Clog Unisex wWT8RaRqz objects as we don't need to check which Shoes Camo Blue Classic Garden Base Ultifree Clog Unisex wWT8RaRqz called the function anymore:

void checkTime() // [9]
{
auto currentTime = Time::getCurrentTime();
auto includeDate = true;
auto includeTime = true;
auto currentTimeString = currentTime.toString (includeDate, includeTime);
timeLabel.setText (currentTimeString, Meleen Sandal Crocs Twist Mushroom Gold Women's 8rrZnxf);
}
Note
The implementation of the code can be found in the ListenersAndBroadcastersTutorial_04.h file of the demo project for this tutorial.

Closed Jess Laforst Leather Womens Toe Metal Mules In this tutorial we have introduced the basics of the broadcaster-listener system in JUCE. While we have focused on buttons in this tutorial, the same techniques can be applied to many areas of JUCE code. In particular we have learned: