Adding submenus to a ComboBox

#1

Hi There,

I’m wanting to add sub menus to a ComboBox.

I’ve found out how to do this to a PopupMenu, which is what’s inside a ComboBox.

There’s this little nugget in ComboBox:

    /** Returns the PopupMenu object associated with the ComboBox.
    Can be useful for adding sub-menus to the ComboBox standard PopupMenu
*/
PopupMenu* getRootMenu() { return &currentMenu; }

But nothing further that I can find.
A combo box has addItem which can only add a PopupMenu item.
There doesn’t seem to be a way to add a subMenu.
Do I create the popup menus and add them into the combo box?

Thank you!

0 Likes

#2

Look up PopupMenu. That’s the object you can add items and sub-menus to.

0 Likes

#3

yes I saw that, but how do I set the combobox to use the popupo menu?

0 Likes

#4

How about like this?

PopupMenu subMenu;
subMenu.addItem(id, “Sub Menu Item Name”);

mainMenu.getRootMenu()->addSubMenu(“Sub Menu Title”, subMenu);.

0 Likes

#5

Yeah that’s how to add sub menus to a pop up menu.

How do you add the pop up menu to a combo box?

0 Likes

#6

Hi @nigelstanford,

For doing that, personally I add menu item with name owning a special separator like that:
“menu1/submenu2”
“menu1/submenu2”
“menu2/submenu1”
“menu2/submenu2”
“menu2/submenu2/item1”
“menu2/submenu2/item2”

Then I create a new TreeStructuredComboBox class that inherit from ComboBox and override
virtual void ComboBox::showPopup();
On this method I iterate on ComboBox items with
PopupMenu::MenuItemIterator iterator(*getRootMenu(), true); and create a new PopupMenu according to rootMenu and separators. then I show the newly created menu instead of the rootMenu one with PopupMenu::showMenuAsync() like in the ComboBox::showPopup() code.

Hopping all of this can helps
The best.

0 Likes

#7

https://docs.juce.com/master/classComboBox.html#ada8ab191b09811f989a0df42e26dcd8b


PopupMenu* ComboBox::getRootMenu	(		)	
Returns the PopupMenu object associated with the ComboBox.

Can be useful for adding sub-menus to the ComboBox standard PopupMenu
0 Likes

#8

so wait, I use getRootMenu() to get the Popup menu, then I can add items to it from there?

0 Likes

#9

That’s what the documentation says to do! Good luck!

0 Likes

#10

You can add a sub menu(PopupMenu) to a combobox with getRootMenu()->addSubMenu().

0 Likes

#11

awesome, thank you all!

0 Likes