[SOLVED ] getToggleState() and radio buttons on Juce

Hi everyone,
I have a question regarding getToggleState().

I added a few radio buttons to be used for triggering some changes in the sound.
I used getToggleState() but It seems like I am forgetting something; when I select a radio button it doesn’t deselect if i click it again. I got some help from Juce tutorials but I think I didn’t quite understand getToggleState()
That’s my code:

 enum RadioButtonIds
{
    firstButtons = 1001,
    secondButtons = 1002,
    thirdButtons = 1003,
    fourthButtons = 1004,
    fifthButtons = 1005
};

void paint (Graphics&) override;
void resized() override;


void updateToggleState (Button* button, String name)
{
    auto state = button->getToggleState();
    String stateString = state ? "ON" : "OFF";
    
    Logger::outputDebugString (name + " Button changed to " + stateString);
}


void normalToggleState (Button* button, String name)
{
    auto state = button->getToggleState();
    String stateString = state ? "ON" : "OFF";
    String selectedString = state ? " [active]" : "";
    
    Logger::outputDebugString (name + " Button changed to " + stateString);
    button->setButtonText (name + selectedString);
}

and in the MainComponentConstructor:

addAndMakeVisible (firstButton);
addAndMakeVisible (secondButton);
addAndMakeVisible(thirdButton);
addAndMakeVisible(fourthButton);
addAndMakeVisible(fifthButton);
addAndMakeVisible(fifthButton);

dimButton .onClick = [this] { normalToggleState (&dimButton, "DIM"); };
airButton .onClick = [this] { normalToggleState (&airButton, "AIR"); };
phaseButton .onClick = [this] { normalToggleState (&phaseButton, "Phase"); };
hpfButton .onClick = [this] { normalToggleState (&hpfButton, "HPF"); };
micInButton .onClick = [this] { updateToggleState (&micInButton, "Mic Input"); };
lineInButton .onClick = [this] { updateToggleState (&lineInButton, "Line Input"); };

    
dimButton .setRadioGroupId (DimButtons);
airButton .setRadioGroupId (AirButtons);
phaseButton .setRadioGroupId (PhaseButtons);
hpfButton .setRadioGroupId(HPFButtons);
micInButton .setRadioGroupId (InputButtons);
lineInButton .setRadioGroupId (InputButtons);

airButton   .setBounds (500, 40, getWidth() - 30, 20);
dimButton   .setBounds (500, 70, getWidth() - 30, 20);
phaseButton .setBounds (500, 100, getWidth() - 30, 20);
hpfButton   .setBounds (500, 130, getWidth() - 30, 20);
micInButton .setBounds (500, 160, getWidth() - 30, 20);
lineInButton .setBounds (500, 190, getWidth() - 30, 20);

Thank you all!

If I’m not completely misunderstanding your question - you are hoping that a radio button will be selected upon first click and deselected if you click it again? If that’s correct then that is not how the radio buttons work. With radio buttons you have a group of buttons that are grouped according to their radioGroupId number. In this group, one is always selected, so to deselect a button you would need to choose one of the others in the group. Being able to toggle a button on/off each time you click it is the behaviour of a toggle button instead. :wink:

1 Like

Simpler than I thought. Thank you very much for your precious help! Sometimes It’s simpler than it actually seems, nice one!

1 Like