Unit 5 · Lesson 314 minAcademic review pending

A menu is a nested collection, with listeners like any button

A JMenuBar holds JMenus, each holding JMenuItems — the same containment pattern from every layout this unit covered. And a JMenuItem responds to clicks exactly like a JButton, because underneath, it essentially is one.

Choose explanation

After this lesson

You should be able to

  • Build a menu bar with at least one menu and several menu items.
  • Attach an ActionListener to a JMenuItem, the same way as a JButton.
  • Explain the difference between a regular menu and a popup menu.
01

Nesting menus the same way you nested layouts

A JMenuBar attaches to a JFrame and holds one or more JMenu objects — "File", "Edit", and so on. Each JMenu holds JMenuItem objects, the actual clickable entries like "Save" or "Open". This three-level containment — bar holds menus, menus hold items — is the same nesting principle as a BorderLayout's Center region holding a GridLayout panel from the layout managers lesson, just applied to menus instead of buttons.

JCheckBoxMenuItem and JRadioButtonMenuItem behave exactly like their non-menu counterparts from the first Swing lesson, just placed inside a menu instead of directly on the window — a toggleable option or a mutually exclusive choice, reachable from the menu bar rather than sitting permanently visible on screen.

Three levels: bar, menu, item — listeners attach at the item level
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");

JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");

openItem.addActionListener(e -> System.out.println("Open clicked"));
saveItem.addActionListener(e -> System.out.println("Save clicked"));

fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();   // a JSeparator: a visual dividing line, nothing more

menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);
02

Popup menus: the same idea, shown on demand

A JPopupMenu holds JMenuItems exactly like a JMenu does, but it is not permanently attached to a menu bar — it appears only when explicitly shown, typically in response to a right-click, and disappears once the user picks an option or clicks elsewhere. Everything about building it — creating items, attaching listeners — is identical to a regular menu; only when and how it becomes visible differs.

This is the event-driven model from Unit IV again, one level further: a mouse listener detects the right-click event, and its handler calls show() on the popup menu at the click's coordinates — the popup itself does not decide when to appear, your listener code does.

Try it yourself

Build a JPopupMenu with two items, "Cut" and "Copy", each printing its own name when clicked. Attach a mouse listener to a panel so that right-clicking it shows the popup menu at the click location.

Need a hint?

The MouseAdapter method for a right-click is mousePressed or mouseReleased, checking e.isPopupTrigger() — showing the popup is a call to show(component, x, y) using the event's own coordinates.

Check the worked solution

Detecting the right-click through a MouseAdapter and calling show() with the event's own x and y coordinates places the popup exactly where the user clicked — reusing the mouse-event mechanics from earlier in this unit, now driving a popup menu instead of printing a coordinate to the console.

JPopupMenu popup = new JPopupMenu();
JMenuItem cutItem = new JMenuItem("Cut");
JMenuItem copyItem = new JMenuItem("Copy");

cutItem.addActionListener(e -> System.out.println("Cut"));
copyItem.addActionListener(e -> System.out.println("Copy"));

popup.add(cutItem);
popup.add(copyItem);

panel.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        if (e.isPopupTrigger()) {
            popup.show(panel, e.getX(), e.getY());
        }
    }
});

Quick check

Why does adding an ActionListener to a JMenuItem use the exact same method and pattern as adding one to a JButton?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

Menu Basics, Menu related classes - JMenuBar, JMenu, JMenuItem, JCheckBoxMenuItem, JRadioButtonMenuItem, JSeperator. creating a popup menu.

Maps to course outcome CO4.