Ask Questions?

View Latest Questions


 
 

Menus
Posted on: July 26, 2006 at 12:00 AM
Think of a menu as a way to arrange buttons.

Java: Menus

Types of menus

Think of a menu as a way to arrange buttons. There are several types.

  • Dropdown menus hang down from the menu bar at the top of a window.
  • Popup menus appear when the user clicks, eg with the right mouse button, on a component that can handle a popup request.
  • Combo boxes are like menus that always shows one item. Similary, there are lists and radio buttons.

Dropdown menus: JMenuBar, JMenu, and JMenuItem

A menu bar can be added to the top of any top-level containers, eg, JFrame, JApplet, or JDialog. Note that a menu bar can not be added to JPanel.

Dropdown menus have three parts:

  1. JMenuBar is positioned across the top of a container (eg a JFrame, JPanel, or JApplet). It's placed above the content pane, so does not use the container's layout. Add menus to the menubar.
  2. JMenu is a vertical list of menu items.
  3. JMenuItem and Separators are added to each menu. Menu items are usually strings, but can also have icons, checkboxes, or hierarchical submenus.

Menus and Menu Items are Buttons!

It is easy to see how menu items are buttons that appear when a menu appears. But the menu names in the menu bar are also buttons. When you press on these "buttons", they create a popup menu that you see as a dropdown menu.

Example - MenuDemo

The following program creates a simple menu. It doesn't do anything useful.

The main program

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
// File   : menus/MenuDemo.java
// Purpo