# Disabling items in menubar

**URL:** <https://forum.juce.com/t/disabling-items-in-menubar/57917>\
**Category:** Development\
**Created:** [September 18, 2023, 5:02am UTC](https://forum.juce.com/t/disabling-items-in-menubar/57917 "2023-09-18T05:02:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![radiance32](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@radiance32](https://forum.juce.com/u/radiance32)\
**Post date:** [September 18, 2023, 5:02am UTC](https://forum.juce.com/t/disabling-items-in-menubar/57917/1 "2023-09-18T05:02:51Z")

</div>

Hi,

I’m using a simple main menu (File, Edit, View, About, etc…) in my JUCE app with a mainmenubar model that contains all my menu entries and sub-entries.

I have an undo/redo system and it also keeps track of if the user has made any changes since the last save to file.

If changes were made, the user can select File → Save to save it to the file saved to before or opened from before. If the user tries to exit the app, the app will ask if you want to save changes or not before exiting.

If no changes have been made, there’s no need to do a File → Save, so I would like to have the File → Save menu entry disabled. (eg I’ll make it a darker colour and it should’nt be selectable). This is what many commercial windows applications do. They also won’t ask to do a save on exitting the app.

So, how can I easily change one of my existing menubarmodel items (in this case File → Save) to look disabled and it being unselectable ?

Here’s a snippet of code for my menubarmodel:

juce::StringArray MainMenuBarComponent::getMenuBarNames()  
{  
return juce::StringArray(“File”,“Edit”, “View”, “About”);  
}

juce::PopupMenu MainMenuBarComponent::getMenuForIndex(int n, const juce::String& menuName)  
{  
PopupMenu menu;

```
if (menuName == "File")
{
    menu.addItem(1, "New...");
    menu.addItem(2, "Open...");
    menu.addItem(3, "Save");
    menu.addItem(4, "Save as...");
    menu.addItem(5, "Exit");
}
else if (menuName == "Edit")
{
    menu.addItem(6, "Undo");
    menu.addItem(7, "Redo");
    menu.addItem(8, "Project Preferences...");
}
else if (menuName == "View")
{
    menu.addItem(9, "Zoom In");
    menu.addItem(10, "Zoom Out");
}
else if (menuName == "About")
{
    menu.addItem(11, "About Hummingbird...");
}

return menu;

```

}

void MainMenuBarComponent::menuItemSelected(int menuItemID, int topLevelMenuIndex)  
{  
switch (menuItemID)  
{  
case 1: // New…  
rootManager-\>NewProject(true);  
break;  
case 2: // Open…  
rootManager-\>LoadProjectFromFile();  
break;  
case 3: // Save  
rootManager-\>SaveProjectToFile();  
break;  
case 4: // Save as…  
rootManager-\>SaveAsProjectToFile();  
break;  
…

That’s it…

Thanks for your time, it’s much appreciated…

Cheers,  
Terrence

---

<div class="post-metadata">

**Author:** ![stephenk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/stephenk/32/15198_2.png) [@stephenk](https://forum.juce.com/u/stephenk)\
**Post date:** [September 18, 2023, 6:33am UTC](https://forum.juce.com/t/disabling-items-in-menubar/57917/2 "2023-09-18T06:33:02Z")

</div>

Are you using the ApplicationCommandManager? If so, in your getCommandInfo() function, you can set the result that is passed in with result.setActive(false) if you want a particular menu item disabled.

However, from the code I suspect you are not. So in that case, you could make a function to update the menu items and use a `PopupMenu::MenuItemIterator` to loop through the items in your popups menus and set them to enabled/disabled with `Item::setEnabled()`.

[https://docs.juce.com/master/structPopupMenu\_1\_1Item.html#ac1cf39dd10c639161cd8e8562e508bee](https://docs.juce.com/master/structPopupMenu_1_1Item.html#ac1cf39dd10c639161cd8e8562e508bee)

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [September 18, 2023, 1:42pm UTC](https://forum.juce.com/t/disabling-items-in-menubar/57917/3 "2023-09-18T13:42:25Z")

</div>

You perhaps have not noticed that `menu.addItem()` has additional parameters.

`juce::PopMenu::addItem(int itemResultID, juce::String itemText, bool isEnabled = true, bool isTicked = false)`

The `isEnabled` parameter is the one you want.

---

<div class="post-metadata">

**Author:** ![radiance32](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@radiance32](https://forum.juce.com/u/radiance32)\
**Post date:** [September 19, 2023, 1:14am UTC](https://forum.juce.com/t/disabling-items-in-menubar/57917/4 "2023-09-19T01:14:31Z")

</div>

No not using any applicationcommandmanager…

---

<div class="post-metadata">

**Author:** ![radiance32](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@radiance32](https://forum.juce.com/u/radiance32)\
**Post date:** [September 19, 2023, 1:15am UTC](https://forum.juce.com/t/disabling-items-in-menubar/57917/5 "2023-09-19T01:15:07Z")

</div>

Thanks,

I manager to get it working with the additional bool isEnabled = true menuitem 🙂

Thanks a lot, problem solved!,  
Terrence
