Parent / Child Mouse Event - Radio Button

As I’m learning this framework, I’m questioning a lot of things.

At the moment, I’m struggling with what seems a trivial task.

In this example, I have a ‘ChannelContainer’ component, which has 8 children, called ‘Channel. Each child, has a ’ mouseDown (const MouseEvent& e)’ method which simply updates a selected Boolean variable.

The intended selection style is a radio button.

void Channel::mouseDown(const MouseEvent& e)

{

isSelected = true;

}

What I want is for the parent to register which ‘child’ has been selected and change its background accordingly.
My following example works… however I’m not sure if this is the correct way about it.

This is my ‘ChannelContainer’ constructor, where I’m initializing each channel:

ChannelContainer::ChannelContainer()
{
    for (int i = 0; i < sizeof(channel) / sizeof(Channel); i++)
    {
        addAndMakeVisible(channel[i]);
        channel[i].addMouseListener(this, false);
    }
}

Then in the ‘mouseDown’ callback, I’m doing the following:

void ChannelContainer::mouseDown (const MouseEvent& e)
{
    if (!(e.eventComponent == this))
    {
        for (int i = 0; i < sizeof(channel) / sizeof(Channel); i++)
        {
            if (channel[i].isSelected) {
                channel[i].isSelected = false;
                channel[i].setColor(Colour(90,90,90));
            } else {
                channel[i].setColor(Colour(67,67,67));
            }
            
            channel[i].repaint();
        }
    }
}

I’m restricting the selection to only each channel, and not the container itself by:

if (!(e.eventComponent == this))

Any suggestions / critiques welcome.
Thanks

You can make your life easier, if you use a subclass of Button for your Channel.

Set setClickingTogglesState (true); on each of them and assign the same number as setRadioGroupId (100); (the number is arbitrary but not 0 IIRC). If they have the same parent, they will automatically be now mutually exclusive.

Instead of the bool flag you had, call getToggleState(); to see, if it is selected.

There are different buttons, TextButton, ImageButton, so you can make it look exactly like you want.

I would stay away from mouseListener, as much as I can. It can create a mess, since it makes the event appear twice in different components. In 95% there is a better way.

Thanks Daniel. I’ll look into that.