popMenu does't appear

Hi, I tried to create PopupMenu but nothing happens:

void buttonClicked(Button* b) override
{
    PopupMenu menu;
    PopupMenu::Item item1;
    PopupMenu::Item item2;
    
    item1.text = "item1";
    item2.text = "item2";

    menu.addItem(item1);
    menu.addItem(item2);

    menu.showMenuAsync(juce::PopupMenu::Options());
}

I’d like to achieve the result of right click menu that happens in every pc/mac enviroments (here for semplicity is done with left click, but an example shoud be right clicking on desktop of mac/pc).

how can I do?

Based on your description (here for semplicity is done with left click) , I’m guessing that this should be inside of a Component::mouseDown() override, not a Button::Listener::buttonClicked() override?

1 Like

thank you for reply, but I’ve just tried overriding mouseDown in mainComponent and put inside previous code, nothing happens again and instead (as before) error is rised inside juce_PopupMenu.cpp line ~1432:

// An ID of 0 is used as a return value to indicate that the user
// didn’t pick anything, so you shouldn’t use it as the ID for an item…

OK Solved, I need to assign id to Item and it must be greater than 0!

1 Like

the assert is telling you what is wrong. You are adding a menu item with the id of 0, which per the documentation (and the assert) is invalid. You need to set the itemID member for each of your PopupMenu::Item's, and they need to be non-zero. These id’s will be how you know which item of the menu was selected (or a 0 id will be returned if no item was selected).

1 Like

@cpr really thank you!!