Needing Guidance with Combo Boxes

I’ve been fairly successful understanding how to use other components in JUCE, but the combo box has me puzzled. I’m simply trying to give the user 3 or 4 options then test for which of those were selected. I’m writing a small encryption app that gives the users 3 or 4 types of encryption, then when the encryption button is clicked, depending on what type is selected in the combo box, determines what happens in the button handler code. I can’t seem to find the right function calls or set it up correctly. I just have newlines between each item in the list of items in the Projucer and I can’t seem to understand how to test for each of the cases. I’ve tried the comboBox->isItemEnabled(//numberHere) and it seems to always return true no matter what number i put in and no matter which item in the combo is selected. I can’t seem to figure it out.

Any help would be much appreciated.

Depending on your use-case you can use either ComboBox::getSelectedItemIndex(), ComboBox::getSelectedId(), or ComboBox::getText(). For Projucer-generated ComboBox objects then the ID will be the index + 1.

I’m ok with the index value, but for some reason, I can’t seem to test for a specific choice. If I have 2 item in the list, both comboBox->isItemEnabled(1) and comboBox->isItemEnabled(2) both always return true, no matter which option is selected in the GUI.

Oh, sorry I see. I’m pretty sure you can only select one item. But isItemEnabled() is whether that item can be selected. Would a collection of ToggleButton objects be more appropriate for what you’re doing? (If you have only four choices.) Or create your own PopupMenu where you can have multiple item ticked or not ticked (and being enabled is a separate concept).

From my opinion, using Projucer’s GUI designer is not a good choice when you understand JUCE’s Listener/callback things not enough…

In your case, I guess it’s none of the isItemEnabled()'s business. Like martinrobinson said, just using ComboBox::getSelectedId(), or ComboBox::getText() in YourClass::comboBoxChanged() is enough… for example:

if (comboBoxThatHasChanged == comboBoxOne) 
    switch (comboBoxOne->getSelectedId()) 
        case 1:  /*doSomthing...*/; break;
        case 2:  /*doSomthing...*/; break;
        case 3:  /*doSomthing...*/; break;
        default:  break;

these 1, 2, 3…come from:

comboBoxOne->addItem ("1's text to display", 1); 
comboBoxOne->addItem ("2's text to display", 2);
comboBoxOne->addItem ("3's text to display", 3);
// ...
1 Like

Thank you very much @loopfine, that was exactly the call I needed. These classes are so big and often use similar names for different calls, so I sometimes struggle to find what I need. Thanks a ton again.

What is the difference between


I don’t understand why there are even two options for retrieving the current comboBox selection.

One retrieves the index position of the item… and the other retrieves the Id value you assigned to the item.

They both can be useful.

The one method I found missing which I had to subclass ComboBox to add was

int CustomCombo::getItemID (const String szText);

which gets the ID based on the item text. If there are more than one item with the same text value it returns the first occurrence’s ID in my implementation.