0% found this document useful (0 votes)
39 views4 pages

Adv Java Lec-22 Creating Menu.5e67ad2

This document discusses how to create menus using MenuBar, Menu, and MenuItem in Java. It explains that a MenuBar contains the names of pull down menus. Menus can contain MenuItems and submenus. It provides code to build a MenuBar and add Menus and MenuItems. It includes a full program example that demonstrates creating a File, Edit, and Help menu with items like New, Open, Save, Cut, Copy and Paste. When the user selects a menu item, an action listener is triggered to handle the event.

Uploaded by

Gayatri Shimpi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views4 pages

Adv Java Lec-22 Creating Menu.5e67ad2

This document discusses how to create menus using MenuBar, Menu, and MenuItem in Java. It explains that a MenuBar contains the names of pull down menus. Menus can contain MenuItems and submenus. It provides code to build a MenuBar and add Menus and MenuItems. It includes a full program example that demonstrates creating a File, Edit, and Help menu with items like New, Open, Save, Cut, Copy and Paste. When the user selects a menu item, an action listener is triggered to handle the event.

Uploaded by

Gayatri Shimpi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.
[Link](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


[Link](editMenu);

 You add menu items, separators, and submenus to the menu object
MenuItem pasteMenu = new MenuItem(“Paste”);
[Link](pasteMenu);
[Link]();

Menu optionMenu = …..; // a submenu


[Link](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 = ….;
[Link](listener);

 There is a convenient method [Link](String s) that holds a menu


item to the end of a menu, for example
[Link](“Paste”);

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

Program to demonstrate MenuBar, Menu and MenuItem:


import [Link].*;
import [Link].*;

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");

[Link](n); //adding menuitems under menus


[Link](open);
[Link](save);
[Link]();
[Link](op); //adding options as submenu of file menu
[Link]();
[Link](exit);
[Link](read);
[Link](write);

[Link](cut);
[Link](copy);
[Link](paste);

[Link](about);

[Link](file); //adding menus on Menubar


[Link](edit);
[Link](help);

setMenuBar(mb); //setting MenuBar on frame

[Link](this);
[Link](this);
[Link](this);

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

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

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

You might also like