You are on page 1of 4

Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Adv. Java Lecture-22


Topic: Creating Menu using MenuBar, Menu and MenuItem
 AWT also support another type of user interface elements, the pull
down menus that are familiar from GUI applications.

 A menu bar on top of the window contains the names of the pull down
menus. Clicking on a name opens the menu containing menus items and
submenus. When the user clicks on a menu item, all menus are closed
and a message is sent to the program.

Building Menus:
 Building menus is straightforward. You create a menu bar
MenuBar menuBar = new MenuBar();

 Then, you can add it menu bar on the top of frame using the
setMenuBar() method.
frame.setMenuBar(menuBar);

 For each menu, you create a menu object


Menu editMenu = new Menu(“Edit”);

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

 You add the top level menus to the menu bar


menuBar.addMenu(editMenu);

 You add menu items, separators, and submenus to the menu object
MenuItem pasteMenu = new MenuItem(“Paste”);
editMenu.add(pasteMenu);
editMenu.addSeparator();

Menu optionMenu = …..; // a submenu


editMenu.add(optionMenu);

 When the user selects a menu, an action event is triggered. You need to
install an action listener for each menu item.
ActionListener listener = ….;
pasteItem.addActionLisener(listener);

 There is a convenient method Menu.add(String s) that holds a menu


item to the end of a menu, for example
editMenu.add(“Paste”);

 The add method returns the created menu item, so that you can capture
it and then add the listener, as follows
MenuItem pasteItem = editMenu.add(“Paste”);
pasteItem.addActionListener(listener);

Program to demonstrate MenuBar, Menu and MenuItem:


import java.awt.*;
import java.awt.event.*;

class MenuTest extends Frame implements ActionListener


{
MenuBar mb = new MenuBar();
Menu file = new Menu("File");
Menu edit = new Menu("Edit");
Menu help = new Menu("Help");
Menu op = new Menu("Options");

MenuItem n = new MenuItem("New", new MenuShortcut(KeyEvent.VK_N,false));

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

CheckboxMenuItem open = new CheckboxMenuItem("Open");


MenuItem save = new MenuItem("Save");
MenuItem exit = new MenuItem("Exit");
MenuItem cut = new MenuItem("Cut");
MenuItem copy = new MenuItem("Copy");
MenuItem paste = new MenuItem("Paste");
MenuItem about = new MenuItem("About");
MenuItem read = new MenuItem("Read");
MenuItem write = new MenuItem("Write");

MenuTest()
{
setSize(600,500);
setTitle("Menu Test");

file.add(n); //adding menuitems under menus


file.add(open);
file.add(save);
file.addSeparator();
file.add(op); //adding options as submenu of file menu
file.addSeparator();
file.add(exit);
op.add(read);
op.add(write);

edit.add(cut);
edit.add(copy);
edit.add(paste);

help.add(about);

mb.add(file); //adding menus on Menubar


mb.add(edit);
mb.add(help);

setMenuBar(mb); //setting MenuBar on frame

n.addActionListener(this);
open.addActionListener(this);
exit.addActionListener(this);

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

save.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("Exit"))
System.exit(0);
else
setTitle(s+" Menu Item is selected");
}
public static void main(String args[])
{
MenuTest f = new MenuTest();
f.setVisible(true);
}
}
Output:

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like