Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.


JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz

Bookmark and Share

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


<p>// File   : menus/MenuDemo.java
// Purpose: Simple demo of building menus.  main program.
// Author : Fred Swartz
// Date   : 2000-04-26 (Rota), 2002-05-01 (Sicilia), 2005-02-08

import javax.swing.*;

/////////////////////////////////////////////////////////////////// MenuDemo
public class MenuDemo {
//================================================================= main
    public static void main(String[] args) {
        JFrame win = new JFrame("MenuDemo");

        //... Create the GUI component
(JPanel), which
        //    also contains a
method to create a menubar.
        MenuDemoGUI content = new
        <span class="hilite">




<p>// File   : menus/MenuDemoGUI.java
// Purpose: GUI for menu demo.  Subclasses JPanel and defines a method
//          which generates
// Author : Fred Swartz
// Date   : 2000-04-26 (Rota), 2002-05-01 (Sicilia), 2005-02-08

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

/////////////////////////////////////////////////////////////// MenuDemoGUI
class MenuDemoGUI extends JPanel {
    JTextArea m_editArea = new JTextArea(20, 50);

    //... <span class="hilite">Menuitems</span> are buttons, so
should be instance variables
    //    so they can be en-/disabled by various
    JMenuItem m_openItem = new JMenuItem("Open"); // create new
menu item
    JMenuItem m_quitItem = new JMenuItem("Quit"); // another menu
    JMenuItem m_copyItem = new JMenuItem("Copy");
    JMenuItem m_pasteItem= new JMenuItem("Paste");

    public MenuDemoGUI() {
        //... <span class="hilite">Add
listeners to menu items</span>
        //... Copy and Paste don't have
listeners yet, so disable them.

        //... Add the single component.
        setLayout(new BorderLayout());
        add(m_editArea, BorderLayout.CENTER);

    // It's common to write a method that produces the menubar.
    // There are two reasons for this.
    // First, it gives a little better organization to the
    //        and doesn't mix
content pane and menu construction.
    // Second, it can be defined wherever you want, so that it
can be
    //        put in a JPanel
subclass (as here), or elsewhere.
    <span class="hilite">JMenuBar createMenubar()</span> {
        //... Menubar, menus, menu items. 
Use indentation to show structure.
        JMenuBar menubar = new JMenuBar(); 
// declare and create new menu bar
fileMenu = new JMenu("File");// declare and create new menu
menubar.add(fileMenu);        // add the menu
to the menubar
fileMenu.add(m_openItem); // add the menu item to the menu
fileMenu.addSeparator();  // add separator line to menu
editMenu = new JMenu("Edit");
        return menubar;

    class OpenAction implements ActionListener {
        public void
actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Sorry, can't open anything");

    class QuitAction implements ActionListener {
        public void
actionPerformed(ActionEvent e) {
System.exit(0);  // terminate this program

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy