How to make a menu bar

I am trying to make a menu bar but this all I have so far:

        MenuBarComponent mainMenuBar;

then in the constructor:


and when resized:

        mainMenuBar.setBounds(0, 0, getWidth(), 20);

That’s all. How can I add items to the menu? Can someone give me a simple example?

You need to subclass MenuBarModel and override its pure virtual methods to set up your menu. Take a look at the MenusDemo class in the WidgetsDemo.cpp file of the JUCE Demo to see an example of how to do this.


Just a question related to this theme : Is there a way to disable the top-level menu bar ?

On OSX the best you can do is to use kiosk mode - there are methods in Desktop for doing that.

Okay I managed to make the menubar and it is working. Now I would like to change the background/text/highlight colors of the menubar. Is it possible?

No it’s not actually the solution that meets my requirements.
The issue I am refering to, is as follows :

  • The main window with a main menu bar and a main content component, is already shown on screen
  • I need to show temporarily a second content component on the same main window, until some selections will be done by the user .
  • I don’t want to show the second component via a new dialog or document window.
  • As long as the second content component is shown, no any other user activity, including menus, is acceptable.

However, top-level menu bar is still enabled and I could not find a way to disable it.

At this moment, I am deleting and re-creating the top-level menu, each time I encounter issues like this.

Well, you’d need to just make your MenuBarModel remove or disable items when you don’t want them displayed.

If you use the ApplicationCommand stuff, it’ll disable things automatically based on the currently focused component, but if not then you can just roll your own menu disabling logic.

I have the same question as in the beginning of the topic. How can I make a menu?

How do you do that? I didn’t found anything like “addItem” or something in MenuBarModel class :((

There’s no such class in WidgetsDemo.cpp (JUCE 7.0.3). There is, however, a class in a file ‘MenusDemo.h’ though I guess it requires building all Demos to see how it’s all connected and how to use it. Also the file is kinda… big and funky. Essentialy I need a simple menu with like two items (e.g. “Close”, “Format C:”).

Take a look at the GUI > MenusDemo in the DemoRunner. Not connected to the Widgets Demo.

The MenuBarModel class discussed in this thread is, iirc, for the application’s minimize/maximize/close buttons. Do you want to add the option to format your C drive to that list? What are the requirements, here?

The MenuBarModel relates to the whole issue of putting up a menubar in Mac or Windows, with drop down menus, submenus and commands, for which you use the ApplicationCommandManager, ApplicationCommandTarget, etc.

See the MenusDemo:

class MenusDemo    : public Component,
                     public ApplicationCommandTarget,
                     public MenuBarModel
    /** A list of the commands that this demo responds to. */
    enum CommandIDs
        menuPositionInsideWindow = 1,

EDIT: I found it quite complicated to get a fully-featured menubar up and running. There are a lot of interconnected pieces and classes.

It’s also highly recommended to examine the source code for The Projucer, which utilizes just about every menubar feature in a fully-fledged app.

1 Like

eh… it’s not what I want than…

I know )) I looked at the code and I couldn’t belive that for some simple menu bar, like “Help”, “About” and “Close” you have to write half thousand lines.

I decided that making my own menu-like component would be easier. In the end all I need is a bar with some text lines, and popup-menus connected to those lines.
P.S. I miss Delphi sometimes ))