# PopupMenu::addItem().setAction() issue

**URL:** <https://forum.juce.com/t/popupmenu-additem-setaction-issue/35991>\
**Category:** General JUCE discussion\
**Created:** [November 4, 2019, 8:35pm UTC](https://forum.juce.com/t/popupmenu-additem-setaction-issue/35991 "2019-11-04T20:35:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [November 4, 2019, 8:35pm UTC](https://forum.juce.com/t/popupmenu-additem-setaction-issue/35991/1 "2019-11-04T20:35:54Z")

</div>

The new `PopupMenu::Item` helper functions, especially the one that lets you set a lambda to be called when you choose a menu item, are pretty cool. However, that lambda is never called if you’re setting the popup menu for a combobox like as follows:

```auto
struct Foo
{
    Foo();
private:
    ComboBox cb;
};

Foo::Foo()
{
    int menuID = 1;
    auto* popupMenu = cb.getRootMenu();
    popupMenu->addItem(PopupMenu::Item("test")
                       .setTicked(false)
                       .setAction([this]()
    {
        DBG( "you clicked test" );
    })
                       .setID(menuID++));
    
    cb.setSelectedId(1); //does not invoke the action
}

```

I believe way down here in MenuWindow `void hide (const PopupMenu::Item* item, bool makeInvisible)` is where the action is invoked:

```auto
           if (resultID != 0
                 && item != nullptr
                 && item->action != nullptr)
                MessageManager::callAsync (item->action);
        }

```

---

<div class="post-metadata">

**Author:** ![thecargocult](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thecargocult/32/10988_2.png) [@thecargocult](https://forum.juce.com/u/thecargocult)\
**Post date:** [December 20, 2023, 7:11am UTC](https://forum.juce.com/t/popupmenu-additem-setaction-issue/35991/2 "2023-12-20T07:11:35Z")

</div>

Just ran into this. Very annoying. There’s no triggerActionForSelectedItem() or any other way to easily perform the selection of an item programmatically.

I’m currently writing complicated recursive lambdas to try and find the correct menu item inside submenus inside submenus… and trigger its action.

I won’t even start ranting about the perennial confusion I suffer with ID vs Index.

---

<div class="post-metadata">

**Author:** ![thecargocult](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thecargocult/32/10988_2.png) [@thecargocult](https://forum.juce.com/u/thecargocult)\
**Post date:** [December 20, 2023, 7:13am UTC](https://forum.juce.com/t/popupmenu-additem-setaction-issue/35991/3 "2023-12-20T07:13:11Z")

</div>

Oh a FTR:  
We can’t use the ChangeListener callbacks because they don’t get called when the user re-selects the item already selected. This is a very common scenario in (for example) a preset menu where the settings have changed but the user wants to reselect the preset to get back to it’s correct settings.

---

<div class="post-metadata">

**Author:** ![MBO](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mbo/32/5045_2.png) [@MBO](https://forum.juce.com/u/MBO)\
**Post date:** [December 20, 2023, 8:08am UTC](https://forum.juce.com/t/popupmenu-additem-setaction-issue/35991/4 "2023-12-20T08:08:36Z")

</div>

For the “re-selection” problem:

> [@How to add a function that will be triggered when the same item has been selected in the combobox?](https://forum.juce.com/t/how-to-add-a-function-that-will-be-triggered-when-the-same-item-has-been-selected-in-the-combobox/40960/2):
>
> You can do it in some tricky but very simple way wink After you execute your code (if you have any) in your onChange callback, you should also run this code: comboBox.onChange = [this] { auto menu = comboBox.getRootMenu(); auto id = comboBox.getSelectedId(); juce::PopupMenu::MenuItemIterator iterator (\*menu); while (iterator.next()) { auto item = &iterator.getItem(); if (item-\>itemID == id) { item-\>setAction ([this] { /\* y…

I think it should be a default behaviour, just for the reasons you mentioned.
