How to Create a Menu:-

A JMenu is different from other components because you cannot add a JMenu component to ordinary containers and have them laid out by the layout manager. You can add menus only to a container. This section describes the following procedure to create a menu:

  1. Create a JMenuBar object, and set it into a menu container, such as a JFrame.
  2. Create one or more JMenu objects, and add them to the menu bar object.
  3. Create one or more JMenuItem objects, and add them to the menu object.

Note 1: – Pop-up menus are an exception to this rule because they appear as floating windows and, therefore, do not quire layout.

Note 2: – With AWT, you could designate one menu to be the Help menu. This was specified by using the setHelpMenu method. This method is not implemented in Swing and Help menus are added hust like any other menu item.

Creating a JMenuBar:-

  • A JMenuBar component is a horizontal menu. You can add it to a JFrame object only, and it forms the root of all menu trees.
  • A JFrame displays one JMenuBar component at a time. However, you can change the JMenuBar based on the state of the program so that different menus appear at various points.

Simple code for JMenuBar:-

  1. f = new JFrame(“JMenuBar”);
  2. mb = new JMenuBar();
  3. f.setJMenuBar(mb);

Note: – The appearance of the window varies slightly from operating system to operating system.

The JMenuBar does not support listeners. All the events that might arise in a menu bar are processed by the menus that are added to the menu bar.

Creating a JMenu:-

The JMenu component provides a basic pull down menu. You add it either to a JMenuBar or to another JMenu.

Simple code for JMenuBar with Top Level JMenu Items:-

  1. f = new JFrame(“Menu”);
  2. mb = new new JMenuBar();
  3. m1 = new JMenu(“File”);
  4. m2 = new JMenu(“Edit”);
  5. m3 = new JMenu(“Help”);
  6. mb.add(m1);
  7. mb.add(m2);
  8. mb.add(m3);
  9. f.setJMenuBar(mb);

You can add an ActionListener to a JMenu object, but this is unusual. Normally, you use menus to display and control menu items that are described in the following section.

GUI Based Applications

Creating a JMenuItem:-

The JMenuItem components are the text leaf nodes of a menu tree. They are added to a menu to complete it, as shown in the following example.

Simple code for JMenuItem added to a Menu:-

  1. mi1 = new JMenuItem(“New”);
  2. mi2 = new JMenuItem(“Save”);
  3. mi3 = new JMenuItem(“Load”);
  4. mi4 = new JMenuItem(“Quit”);
  5. mi1.addActionListener(this);
  6. mi2.addActionListener(this);
  7. mi3.addActionListener(this);
  8. mi4.addActionListener(this);
  9. m1.add(mi1);
  10. m2.add(mi2);
  11. m3.add(mi3);
  12. m1.addSeparator();
  13. m4.add(mi4);

Usually, you add an ActionListener to a JMenuItem object to provide behavior for the menus.

Creating a JCheckboxMenuItem:-

The JCheckboxMenuItem is a checkable mwnu item, so you can have selection (on or off choices) listed in menus, as shown in the following example.

Demonstration – JcheckboxMeniItem

  1. f = new JFrame(“CheckboxMenuItem”);
  2. mb = new JMenuBar();
  3. m1 = new JMenu(“File”);
  4. m2 = new JMenu(“Edit”);
  5. m3 = new JMenu(“Help”);
  6. mb.add(m1);
  7. mb.add(m2);
  8. mb.add(m3);
  9. f.setJMenuBar(mb);
  10. ……….
  11. mi5 = new JCheckboxMenuItem(“Persistent”);
  12. mi5.addIemListener(this);
  13. m1.add(mi5);

You should monitor the JCheckboxMenuItem using the ItemListener interface.

Controlling Visual Aspects:-

You can control the colors used for the foreground and the background of AWT components.


  • you use two methods to set the colors of a component:



  • Both of these methods take an argument that is an instance of java.awt.Color class.
  • You can use constant colors referred to as,, and so on. The full range of predefined colors is listed in the documentation page for the Color class.
  • You can also construct a specific Color object by specifying the color by a combination of three byte sized integers (0-255), one for each primary color: red, blue, and green. For example:

Color purple = new Color(255,0,255);

JButton b = new JButton(“Purple”);


Written by Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc.

Leave a Comment