A Java menubar example

Java Swing FAQ: How do I create a menubar (and menu and menuitems) in Java?

As I was working on a Java/Swing problem last week, I was scouring through the Mac OS X Java-Dev mailing list, and along the way I ran into some really nice source code I thought I could modify into a decent Java Menubar example. After a little rewriting, here is that "Java Menubar" example.

Java Menubar example - source code

The class I saw was written to demonstrate something else, so after some rewriting, I came up with the source code shown below, which I think is a decent starter Java Menubar example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

 * Written/modified by alvin alexander, devdaily.com.
 * This class was nspired by a thread in the Mac Java-dev mailing list
 * (http://lists.apple.com/archives/java-dev/).
public class JavaMenuBarExample implements Runnable, ActionListener
  private JFrame frame;
  private JMenuBar menuBar;
  private JMenu fileMenu;
  private JMenu editMenu;
  private JMenuItem openMenuItem;
  private JMenuItem cutMenuItem;
  private JMenuItem copyMenuItem;
  private JMenuItem pasteMenuItem;

  public static void main(String[] args)
    // needed on mac os x
    System.setProperty("apple.laf.useScreenMenuBar", "true");

    // the proper way to show a jframe (invokeLater)
    SwingUtilities.invokeLater(new JavaMenuBarExample());

  public void run()
    frame = new JFrame("Java Menubar Example");
    menuBar = new JMenuBar();
    // build the File menu
    fileMenu = new JMenu("File");
    openMenuItem = new JMenuItem("Open");

    // build the Edit menu
    editMenu = new JMenu("Edit");
    cutMenuItem = new JMenuItem("Cut");
    copyMenuItem = new JMenuItem("Copy");
    pasteMenuItem = new JMenuItem("Paste");

    // add menus to menubar

    // put the menubar on the frame

    frame.setPreferredSize(new Dimension(400, 300));

   * This handles the action for the File/Open event, and demonstrates
   * the implementation of an ActionListener.
   * In a real-world program you'd handle this differently.
  public void actionPerformed(ActionEvent ev)
    SampleDialog dialog = new SampleDialog();

   * This dialog is displayed when the user selects the File/Open menu item.
  private class SampleDialog extends JDialog implements ActionListener
    private JButton okButton = new JButton("OK");

    private SampleDialog()
      super(frame, "Sample Dialog", true);
      JPanel panel = new JPanel(new FlowLayout());
      setPreferredSize(new Dimension(300, 200));

    public void actionPerformed(ActionEvent ev)

Java Menubar example - discussion

When you run this Java program, it will display a JFrame in the center of your screen, and a menubar that has two main menus. When you choose any of the Edit menu items nothing will happen, but when you choose the Open menu item under the File menu, you'll see the SampleDialog appear.

When looking at the source code for this Java class, I think it demonstrates several things very nicely:

  • It shows how to set the menubar property for Mac OS X systems.
  • It shows the proper way to launch a JFrame, using SwingUtilities.invokeLater.
  • The process of building the Java Menubar (JMenuBar, JMenu, and JMenuItem) is very clear.

The class also demonstrates how to implement an ActionListener as well, but as a word of caution, this works okay for this very simple class, but in your real-world programs you'll want to create ActionListeners as separate classes, probably extending the Java AbstractAction class.

I thought about modifying this class some more to show you better ways of implementing some of these techniques, but for now, this is a decent example of how to construct a Java Menubar, using the JMenuBar JMenu and JMenuItem classes.