Problem with button behaviour

I have three toggle buttons which I need to be clickable but only one can be active at a time. Getting the to deselect when another button is selected works fine, but the one which is selected looses it’s buttonDown state when the mouse moves off of the button.

How do I make it so the toggle button stays down.

button initialization, the custom button type only has different graphics, nothing else is changed

[i]contentComponent->addAndMakeVisible (Button_Reverb_Styles[2] = new LED_Button_Pressable() );
Button_Reverb_Styles[2]->setBounds(380, 200, 25, 25);
contentComponent->addAndMakeVisible (Button_Reverb_Styles[1] = new LED_Button_Pressable() );
Button_Reverb_Styles[1]->setBounds(414, 200, 25, 25);
contentComponent->addAndMakeVisible (Button_Reverb_Styles[0] = new LED_Button_Pressable() );
Button_Reverb_Styles[0]->setBounds(448, 200, 25, 25);

Trying to set the states properly. Eeach button is bit mapped to a bit in UI_ELEMENT_ENABLE[2]

if ( UI_ELEMENT_ENABLE[2] & 0x01 ) {
} else {
if ( UI_ELEMENT_ENABLE[2] & 0x02 ) {
} else {
if ( UI_ELEMENT_ENABLE[2] & 0x04 ) {
} else {
if ( UI_ELEMENT_ENABLE[2] & 0x08 ) {
} else {

So you basically want something like a radio button group (exactly one button of the group is active at the same time) but with toggle buttons. The JUCE demo has exactly what you want on the radio button tab of the widgets page. The code to set it up looks like this:

[code] for (i = 0; i < 4; ++i)
TextButton* tb = new TextButton (T("button ") + String (i + 1));

    page->addAndMakeVisible (tb);

    tb->setClickingTogglesState (true);
    tb->setRadioGroupId (34567);

    tb->setBounds (20 + i * 55, 260, 55, 24);

    if (i == 0)
        tb->setToggleState (true, false);

The important lines are:

tb->setClickingTogglesState (true); tb->setRadioGroupId (34567);

Making the button toggle its state when clicked (turning it into a toogle button) and giving all buttons of the group the same radio group id so JUCE takes care of handling the button states in a way that they form a toggle button group.

You should not use the state of the button, because that’s going to interfere with the mouse handling (that may be the problem you see with your approach), but you should use the toggle state, as the JUCE demo and the example code above does. So just use Button::getToggleState in the paint method of your custom button instead of Button::isDown to decide how to draw the button, when you use the toggle state approach.

Thanks photon, very elegantly explained.

I should also mention that you can set up a bunch of toggle buttons like this in the jucer, too - there’s a field on them to let you enter a button group number.

Thanks for that great explanation.

I always forget to check out the demo projects when I find problems.

I thought this would solve my problem as well, but after looking at the JUCE demo it seems like it’s just a normal radio button behavior. What I’m looking for is a way to make 4 buttons with radio button behavior (exclusive selection), BUT if you press the button who’s already pressed it would get deselected/toggled to off. Is there a simple way to do this?

How do you store multiple instances of the TextButton in one variable (tb in this example)?


I am stuck here:

ScopedPointer<ToggleButton> toggleButton;

addAndMakeVisible(toggleButton = new ToggleButton("new toggle button"));
toggleButton->setBounds(150, 50, 150, 24);