Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

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
// Purpose: Simple demo of building menus.  main program.
// Author : Fred Swartz
// Date   : 2000-04-26 (Rota), 2002-05-01 (Sicilia), 2005-12-01 (Pfalz)

import javax.swing.*;

////////////////////////////////////////////////////////////// MenuDemo
public class MenuDemo {
    //============================================================ main
    public static void main(String[] args) {
        JFrame win = new MenuDemoGUI();
        win.setVisible(true);
    }
}

The GUI

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
// File   : menus/MenuDemoGUI.java
// Purpose: GUI for menu demo.  Subclasses JFrame and defines a method
//          which generates menubar.
// Author : Fred Swartz
// Date   : 2000-04-26 (Rota), 2002-05-01 (Sicilia), 2005-12-01 (Pfalz)

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

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

    //... Menuitems are buttons. They are instance variables
    //    so they can be en-/disabled by various actions.
    private JMenuItem m_openItem = new JMenuItem("Open"); // create new menu item
    private JMenuItem m_quitItem = new JMenuItem("Quit"); // another menu item
    private JMenuItem m_copyItem = new JMenuItem("Copy");
    private JMenuItem m_pasteItem= new JMenuItem("Paste");

    //========================================================== constructor
    public MenuDemoGUI() {
        //... Add listeners to menu items
        m_openItem.addActionListener(new OpenAction());
        m_quitItem.addActionListener(new QuitAction());

        //... Copy and Paste don't have listeners yet, so disable them.
        m_copyItem.setEnabled(false);
        m_pasteItem.setEnabled(false);

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

        //... Set content pane and menu bar.
        this.setContentPane(content);
        this.setJMenuBar(createMenubar());

        //... Finish the window.
        this.setTitle("Menu Demo");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
    }


    //==================================================== createMenubar
    // It's not uncommon to write a method that produces the menubar.
    // It gives a little better organization to the program
    // and doesn't mix content pane and menu construction, but
    // there is no real need to separate it.

    private JMenuBar createMenubar() {
        //... Menubar, menus, menu items.  Use indentation to show structure.
        JMenuBar menubar = new JMenuBar();  // declare and create new menu bar
            JMenu 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
                fileMenu.add(m_quitItem);
            JMenu editMenu = new JMenu("Edit");
                menubar.add(editMenu);
                editMenu.add(m_copyItem);
                editMenu.add(m_pasteItem);
        return menubar;
    }


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

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

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.