ComboBox: update items before Menu shows

I’m using ComboBox as a drop-down menu with options to select, save, and open presets (among other things). I would like the ComboBox to re-scan all presets before it’s shown to the user when they select the dropdown.

I tried to override Component::mouseDown, but when this is overridden the menu doesn’t show when clicked. The listener isn’t called unless the selection is changed.

Is there a straightforward way to update a ComboBox menu before it’s shown when selected?

Hi there, I think you could override showPop():

void showPopup() override
{
    getRootMenu()->addItem ("test", nullptr); // update menu here
    ComboBox::showPopup();
}
1 Like

For this scenario you could also use the PopupMenu instead of overriding ComboBox functions.

You can open one from a button like this:

button.onClick = [&]()
{
  juce::PopupMenu menu;
  menu.addItem("Item 1", nullptr);
  menu.addItem("Item 2", nullptr);
  menu.showMenuAsync(juce::PopupMenu::Options{}.withTargetComponent(button));
}

This is a very rough basic example, but you should be able to play around with the options and look up some other examples to get what you’re after.

You can think of this as building a combobox from parts, but I find it easier to work this way for things like preset or options menus.

2 Likes

Great advice! I got it working with that showPop()override!

1 Like