Concepts / Responding to Menu Selections

Responding to Menu Selections

A JMenuBar is a container for menus with no name and a no-parameter constructor; menus are placed in it with add(JMenu).

  • Programming

Why a Menu Strip Can Be Missing

Imagine you run a drawing program. The window has a large area full of drawing tools, but there is no menu strip along the top. The menus may exist in the code, yet nothing appears on screen. This section follows the code that builds a menu strip and attaches it to a window, so you can see exactly where a menu setup can fail to show up.

The running example is the MosaicDraw program. It uses three menus, named controlMenu, colorMenu, and toolsMenu. Each menu is created first. The remaining work is to place those menus into a menu bar, and then place that menu bar into a window. Before reading the mechanism, decide for yourself where the menu bar belongs in a frame. Think about the content pane you may already know from earlier window code, and whether the menu bar is one of the things that pane holds.

The Menu Bar as a Passive Container

Definition

A menu bar is represented by the class JMenuBar. Think of it as an empty strip that holds menus and nothing else. It has no name, and its constructor takes no parameters, so creating one is a single call with no configuration. The object's job is to hold menus.

Menus are placed into the bar with the add(JMenu) method. Each call to add contributes one menu, and that menu's name then appears in the menu bar. The bar itself is passive: it only stores what you add to it. It does not decide when it is displayed, and it does not decide which window it belongs to.

java
Notes on the code
  • One creation statement builds the empty bar with its no-parameter constructor.
  • Each add call contributes exactly one already-created JMenu to the bar.
  • The three menus were created before this point; the bar only stores references to them.

The source builds the bar with one creation statement followed by three add calls. Each statement does one thing, which makes it easy to check each step on its own. If you are debugging, you can read these four lines and confirm that the bar was created and that all three menus were added to that same bar object.

Where the Menu Bar Sits in a Frame

The menu bar is a separate component of the frame. It is not contained inside the content pane. The content pane holds the main drawing area, while the menu bar sits as its own part of the window. The diagram below shows the containment relationships for the MosaicDraw window, from the frame down to the three menus.

JFramethe windowContent paneholds the drawing areacontrolMenuJMenuJMenuBarseparate component of theframecolorMenuJMenutoolsMenuJMenucontainsholds one menu baradd(JMenu)add(JMenu)add(JMenu)
What contains what? The menu bar is a separate part of the frame, not part of the content pane, and each menu sits inside the bar.

Read the tree from the top. The frame has two separate parts that matter here: the content pane and the menu bar. The three menus are not children of the content pane. They are children of the bar, which is attached to the frame. If a menu is not visible, one question to ask is whether the bar that holds it was ever attached to the frame at all.

Why Attaching Is a Set Operation

A menu bar must be attached to a JFrame with setJMenuBar. The source states the method in its prose as setMenuBar(JMenuBar), but the name you write in Java is setJMenuBar, so use that name. The call takes the bar object and gives it to the frame.

This is a set operation rather than an add operation because a frame holds only one menu bar. Adding menus to a bar is an add operation, since a bar collects many menus. Attaching the bar to a frame is different: you assign one bar to the frame, and assigning a second bar would replace the first rather than join it alongside. The table below contrasts the two operations.

OperationCalled onReceivesHow many allowed
add(JMenu)JMenuBarOne JMenuMany menus collected by the bar
setJMenuBar(JMenuBar)JFrameOne JMenuBarOne menu bar per frame

Adding menus to a bar versus attaching the bar to a frame

JFrameContent panenot the menu bar's homeJMenuBarset as the frame's menu barcontent panesetJMenuBar (set, not add)
Where does the menu bar end up? It is set onto the frame as its one menu bar, separate from the content pane.

Tracing the Build-and-Attach Sequence

The source shows the full setup in the MosaicDraw program. The controller creates the canvas and the menu bar, and the main routine connects both to the window. Walking through the steps in order shows which object each call depends on. The sequence below traces the calls from creation to attachment.

ControllerMenu barJMenuBarMain routineWindowJFrame1. new JMenuBar()2. add controlMenu3. add colorMenu4. add toolsMenu5. setJMenuBar(bar)
In what order are the calls made, and which call attaches the finished bar to the window?
Worked example

Locating the Step That Hides the Menu

A developer reports that the MosaicDraw window opens with no menu strip. The menus were created and added to a bar. Which call in the sequence should be checked first?

  1. 1
    Step 1: Check the creation

    Confirm that a JMenuBar was created with its no-parameter constructor. Without a bar object, there is nothing to hold the menus.

  2. 2
    Step 2: Check the adds

    Confirm that controlMenu, colorMenu, and toolsMenu were each passed to add on that same bar object. Menus added to a different bar do not belong to the bar that will be attached.

  3. 3
    Step 3: Check the attachment

    Confirm that setJMenuBar was called on the frame with that bar. The source's check is whether the bar was attached to the frame and whether the menus were added to that same bar.

Result

If the bar was built and filled correctly, the most likely missing step is the setJMenuBar call on the frame. A bar that is never attached stays invisible, even though all its menus were added.

Before
JMenuBar bar = new JMenuBar();
bar.add(controlMenu);
bar.add(colorMenu);
bar.add(toolsMenu);
// setJMenuBar never called

The bar holds three menus but is not attached to any frame, so no menu strip appears.

After
JMenuBar bar = new JMenuBar();
bar.add(controlMenu);
bar.add(colorMenu);
bar.add(toolsMenu);
frame.setJMenuBar(bar);

The bar is attached as the frame's single menu bar, so the menu strip appears along the top of the window.

Mistakes That Hide a Menu

When a menu does not appear, the cause is usually one of a few steps that were skipped or repeated. Each mistake below leaves a different trace in the program. To identify the failing step, ask which call was not made, or which object it was made on.

  • Common mistake

    Never attaching the bar to the frame

    What it looks like

    The menus are added to the bar, but the code never calls setJMenuBar on the frame.

    Why it fails

    The bar is a passive container. It stays invisible until it is placed in a window, so the menus never reach the screen.

    Do this instead

    Call setJMenuBar on the JFrame with the finished bar object.

  • Common mistake

    Adding menus to a different bar object

    What it looks like

    Menus are added to one JMenuBar, but a different JMenuBar is the one passed to setJMenuBar.

    Why it fails

    Only the bar that is attached to the frame is shown. Menus collected by any other bar never appear.

    Do this instead

    Check that every add call and the setJMenuBar call refer to the same bar variable.

  • Common mistake

    Treating setJMenuBar as one more add

    What it looks like

    Assuming that a second call attaching a bar will sit beside the first one.

    Why it fails

    A frame holds only one menu bar, so attaching the bar is a set operation, not a collection step.

    Do this instead

    Build a single bar with all menus added to it, then set that one bar on the frame.

  • Common mistake

    Placing the menu bar inside the content pane when reasoning about the layout, because the bar is a separate component of the frame

Practice: Find the Broken Step

EASY

A program creates a JMenuBar, adds controlMenu, colorMenu, and toolsMenu to it, and then runs. No menu strip appears. Name the call that is missing, and explain why the three add calls alone are not enough to show the menus.

Hints
  • Trace the sequence from creation to attachment, one call at a time.
  • Ask what object each call acts on: the bar, or the frame.
  • A bar that is never placed in a window stays invisible.

What do you think happens?

What do you think happens to a menu bar that holds menus but is never passed to setJMenuBar?

  • The menus appear in the window anyway

  • The bar stays invisible and no menu strip appears

Reveal answer
Answer:

The bar stays invisible and no menu strip appears.

The source states that a menu bar that is never placed in a window stays invisible. The final step is to give the bar to a window such as a JFrame with setJMenuBar.

Key Takeaways

  • A JMenuBar is a passive container with no name and a no-parameter constructor. Menus are placed into it with add(JMenu), and each call contributes one menu.
  • The menu bar is a separate component of the frame, not part of the content pane. It is attached to a JFrame with setJMenuBar.
  • Attaching is a set operation because a frame holds only one menu bar; you assign the bar to the frame rather than adding it to a collection.
  • To locate a missing menu, trace the calls in order: create the bar, add each menu to that same bar, then call setJMenuBar on the frame. A bar that is never attached stays invisible.