Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

JMenuBar, JMenu, and JMenuItem Classes

This section describes 3 Swing classes, JMenuBar, JMenu, and JMenuItem, to build a menu interface for a frame window. A typical menu interface has a menu bar with multiple menus or menu items. A menu can have multiple sub menus or menu items.

javax.swing.JMenuBar - A Swing class representing the menu bar on the main frame. javax.swing.JMenu objects added a JMenuBar object will be displayed horizontally. Interesting methods of JMenuBar include:

  • JMenuBar() - Constructor to create a menu bar which can be added to a frame window.
  • add(JMenu) - Method to add a new JMenu object to the end of the menu be list.
  • getMenuCount() - Method to return the number of menus in this menu bar.
  • getMenu(int) - Method to return the JMenu object of the specified position index. Of course, index 0 is the first menu.
  • getSubElements() - Method to return an array of MenuElement objects.
  • isSelected() - Method to return true if an element is currently selected in the menu bar.

javax.swing.JMenu - A Swing class representing a user interface menu. A JMenu object can be added to a JMenuBar or another JMenu object to form a menu tree structure. A JMenu object actually has two graphical components, a clickable button displayed in the parent JMenu or JMenuBar and a popup window. When its button is clicked, its pop up window will be displayed. Interesting methods of JMenu include:

  • JMenu(String) - Constructor to create a menu with the specified string as its button text.
  • add(JMenuItem) - Method to add a JMenuItem object to the end of its current menu item list.
  • addSeparator() - Method to add a menu separator to the end of its current menu item list.
  • getItemCount() - Method to return the number of its menu items.
  • getItem(int pos) - Method to return the JMenuItem object of the specified position index. Of course, index 0 is the first menu item.
  • isSelected() - Method to return true if this menu is selected.
  • isTopLevelMenu() - Method to return true if this menu is a top menu. A top menu is a menu added to the menu bar, not to another menu.
  • doClick(int) - Method to simulate a user click on this menu.

javax.swing.JMenuItem - A Swing class representing a user interface menu item. A JMenuItem object can be added to a JMenuBar or another JMenu object in a menu tree structure. A JMenuItem should be associated with a MenuKeyListener object so that tasks can be performed with the menu item is clicked. Interesting methods of JMenuItem include:

  • JMenuItem(String) - Constructor to create a menu item with the specified string as its button text.
  • addMenuKeyListener(MenuKeyListener) - Method to add a MenuKeyListener object to this menu item.
  • setEnabled(boolean) - Method to set this menu item to be enabled or disabled based on the specified Boolean value.

Last update: 2009.

Sections in This Chapter

JMenuBar, JMenu, and JMenuItem Classes

JMenuBarTest.java - Menu Bar Test Program

JMenuTest.java - Menu Test Program

JMenuItemTest.java - Menu Item Test Program

JRadioButtonMenuItemTest.java - Radio Button Menu Item Test Program

JCheckBoxMenuItemTest.java - Check Box Menu Item Test Program

javax.swing.event.MenuListener - Menu Listener Interface

MenuItemActionListenerTest.java - Menu Item Action Listener Test

Item Listener on Radio Button Menu Items

Item Listener on Check Box Menu Items

javax.swing.event.MenuKeyListener - Menu Key Listener Interface

setMnemonic() - Setting Keyboard Mnemonics on Menu Items

setAccelerator() - Setting Keyboard Accelerators on Menu Items

setMnemonic() - Setting Keyboard Mnemonics on Menus

Dr. Herong Yang, updated in 2009
JMenuBar, JMenu, and JMenuItem Classes