Problem with button behaviour


#1

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);
Button_Reverb_Styles[2]->addButtonListener(this);
contentComponent->addAndMakeVisible (Button_Reverb_Styles[1] = new LED_Button_Pressable() );
Button_Reverb_Styles[1]->setBounds(414, 200, 25, 25);
Button_Reverb_Styles[1]->addButtonListener(this);
contentComponent->addAndMakeVisible (Button_Reverb_Styles[0] = new LED_Button_Pressable() );
Button_Reverb_Styles[0]->setBounds(448, 200, 25, 25);
Button_Reverb_Styles[0]->addButtonListener(this);
Button_Reverb_Styles[0]->setState(Button_Reverb_Styles[0]->buttonDown);[/i]

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 ) {
Gender1[0]->setState(Gender1[0]->buttonDown);
} else {
Gender1[0]->setState(Gender1[0]->buttonNormal);
}
if ( UI_ELEMENT_ENABLE[2] & 0x02 ) {
Gender1[1]->setState(Gender1[1]->buttonDown);
} else {
Gender1[1]->setState(Gender1[1]->buttonNormal);
}
if ( UI_ELEMENT_ENABLE[2] & 0x04 ) {
Gender1[2]->setState(Gender1[2]->buttonDown);
} else {
Gender1[2]->setState(Gender1[2]->buttonNormal);
}
if ( UI_ELEMENT_ENABLE[2] & 0x08 ) {
Gender1[3]->setState(Gender1[3]->buttonDown);
} else {
Gender1[3]->setState(Gender1[3]->buttonNormal);
}


#2

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);
}[/code]

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.


#3

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.


#4

Thanks for that great explanation.

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


#5

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?


#6

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

 

I am stuck here:

PluginEditor.h:
private:
ScopedPointer<ToggleButton> toggleButton;
...

PluginEditor.cpp:
addAndMakeVisible(toggleButton = new ToggleButton("new toggle button"));
toggleButton->addListener(this);
toggleButton->setBounds(150, 50, 150, 24);