# Custom MenuBar strange behaviour

**URL:** <https://forum.juce.com/t/custom-menubar-strange-behaviour/14874>\
**Category:** General JUCE discussion\
**Created:** [May 27, 2015, 8:20am UTC](https://forum.juce.com/t/custom-menubar-strange-behaviour/14874 "2015-05-27T08:20:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gammer](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@gammer](https://forum.juce.com/u/gammer)\
**Post date:** [May 27, 2015, 8:20am UTC](https://forum.juce.com/t/custom-menubar-strange-behaviour/14874/1 "2015-05-27T08:20:45Z")

</div>

Hi!

I'm trying to implement my custom menu bar. I created MenuBarComponent object and have set my custom MenuBarModel for it.

For now I want to add some custom look to the menu bar, it's just simple, to change background of item by mouseOver.

I implemented something like that:

```
void MenuLookAndFeel::drawMenuBarItem (Graphics& g,
                                       int width,
                                       int height,
                                       int itemIndex,
                                       const String& itemText,
                                       bool isMouseOverItem,
                                       bool isMenuOpen,
                                       bool isMouseOverBar,
                                       MenuBarComponent& menuBarComponent)

{
    if (isMouseOverItem)
    {
        g.setColour (Colours::green);
    }
    else
    {
        g.setColour (Colours::red);
    }
    g.fillRect (0, 0, width, height);
    g.setColour (Colours::white);
    g.drawText (itemText, 0, 0, width, height, Justification::centred, false);
}
```

But all my items (i.e "File", "Edit", "Options" etc.) get filled with this colour, and I want the only one (i.e "File") item will change it's background by mouseOver action.  
Would you be so kind to help me to solve that problem? Seem like I'm doing something wrong.

&nbsp;

Thanks in advance :)

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [May 27, 2015, 8:45am UTC](https://forum.juce.com/t/custom-menubar-strange-behaviour/14874/2 "2015-05-27T08:45:39Z")

</div>

use itemText to know which item you are drawing.

---

<div class="post-metadata">

**Author:** ![gammer](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@gammer](https://forum.juce.com/u/gammer)\
**Post date:** [May 27, 2015, 12:47pm UTC](https://forum.juce.com/t/custom-menubar-strange-behaviour/14874/3 "2015-05-27T12:47:37Z")

</div>

think it will work, thanks! but it looks a bit hardcode...is any elegant solution exists?

---

<div class="post-metadata">

**Author:** ![gammer](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@gammer](https://forum.juce.com/u/gammer)\
**Post date:** [May 27, 2015, 9:37pm UTC](https://forum.juce.com/t/custom-menubar-strange-behaviour/14874/4 "2015-05-27T21:37:00Z")

</div>

```
By the way, I've tried to implement like this:

void MenuLookAndFeel::drawMenuBarItem (Graphics& g,
                                      int width,
                                      int height,
                                      int itemIndex,
                                      const String& itemText,
                                      bool isMouseOverItem,
                                      bool isMenuOpen,
                                      bool isMouseOverBar,
                                      MenuBarComponent& menuBarComponent)
{
    if (isMouseOverItem)
    {
        g.setColour (Colours::green);
        int itemWidth = 200; // width /menuBarComponent.getModel()->getMenuBarNames().size();
        //int itemX = jmax (0, itemIndex * itemWidth);
        int itemX = 0;
        if (itemText == "Edit")
        {
            itemX = 200;
        }
        else if (itemText == "Options")
        {
            itemX = 400;
        }
        g.fillRect (itemX, 0, itemWidth, height);
    }
    g.setColour (Colours::white);
    g.drawText (itemText, 0, 0, width, height, Justification::centred, false);
}
```

but still have the same problem! It changes background by mouseOver only for the first item!

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [May 29, 2015, 9:42am UTC](https://forum.juce.com/t/custom-menubar-strange-behaviour/14874/5 "2015-05-29T09:42:00Z")

</div>

You can probably do that without a L&F using:

menuBar.setColour (PopupMenu::highlightedBackgroundColourId, Colours::green);

Rail

---

<div class="post-metadata">

**Author:** ![gammer](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@gammer](https://forum.juce.com/u/gammer)\
**Post date:** [May 29, 2015, 2:43pm UTC](https://forum.juce.com/t/custom-menubar-strange-behaviour/14874/6 "2015-05-29T14:43:52Z")

</div>

Your solution is good, thanks a lot!

I'm just a bit dissapointed with cohesion between MenuBar and PopupMenu.... But I wrote example with solid colours just to test. I need to implement custom menu bar filling with gradients, borders, etc. for real.  
  
Can you help me with it too, please?  
&nbsp;
