To create a menu in a Java Swing application, create a JMenuBar, add one JMenu per top-level menu such as File or Edit, fill each menu with JMenuItem objects, and attach the bar to the window with frame.setJMenuBar(menuBar). Each item gets an ActionListener (or an Action) that runs when the user selects it.
Swing code must run on the Event Dispatch Thread, so the examples below start the user interface from SwingUtilities.invokeLater. They compile on any current JDK, including Java 21 and 25.
A Swing menu is built from a small set of components, all in the javax.swing package.
| Class | Role |
|---|---|
JMenuBar | The bar at the top of the window; holds menus |
JMenu | A drop-down menu; can also be nested as a submenu |
JMenuItem | A plain clickable entry |
JCheckBoxMenuItem | An entry with an on/off check mark |
JRadioButtonMenuItem | One-of-many entry, used with a ButtonGroup |
JPopupMenu | A context menu shown on right-click |
JSeparator | A dividing line, added with menu.addSeparator() |
The program creates a window with two menus, a separator, keyboard shortcuts and a mnemonic for each menu.
Choosing File, then New prints the message to the console, and choosing Exit closes the application. The key points are:
setJMenuBar places the bar in the frame's menu slot above the content pane, so it does not overlap your layout.setMnemonic lets users open the menu with Alt plus the letter on Windows and Linux.setAccelerator binds a shortcut that works even when the menu is closed. getMenuShortcutKeyMaskEx() returns Ctrl on Windows and Linux and Command on macOS, so the shortcut follows platform conventions. The older getMenuShortcutKeyMask() is deprecated since Java 10.A JMenu can be added to another JMenu, which creates a cascading submenu.
Keep nesting to one level where you can, because deep cascades are hard to use with a mouse.
Use JCheckBoxMenuItem for settings that are on or off and JRadioButtonMenuItem inside a ButtonGroup for exclusive choices.
A context menu is a JPopupMenu. The simplest way to show it is component.setComponentPopupMenu(popup), which handles the platform-specific trigger (right-click or long press) for you.
When the same command appears in a menu, a toolbar button and a shortcut, define it once as an AbstractAction. The menu item and button then share the text, icon, enabled state and handler.
Calling saveAction.setEnabled(false) greys out both the menu item and the button at once.
Make sure you called frame.setJMenuBar(menuBar) and added at least one menu to the bar. Also check that you build the user interface on the Event Dispatch Thread before calling setVisible(true).
Yes. Pass an Icon to the JMenuItem constructor or call setIcon. For crisp results on high-DPI screens, supply an icon that scales well.
Set the system property apple.laf.useScreenMenuBar to true before creating any Swing component, and the same JMenuBar appears in the macOS screen menu bar.
Swing remains supported and is fine for desktop tools, particularly with a modern look and feel such as FlatLaf. For new cross-platform user interfaces, many teams also consider JavaFX.
Build a Swing menu from a JMenuBar, JMenu and JMenuItem, attach it with setJMenuBar, and run the setup inside SwingUtilities.invokeLater. Add separators, mnemonics and accelerators for usability, use check box and radio items for state, JPopupMenu for context menus, and Action objects when the same command appears in several places.