Menus are very familiar to a programmer in Windows environment. A menu comes with a pull-down list of menu items from which user can select one at a time. When a lot of options in different categories exist to be opted by the user, menus are the best choice as they take less space on the frame. A click on the MenuItem generates ActionEvent and is handled by ActionListener. Java AWT Menu and MenuItem are not components as they are not subclasses of java.awt.Component class. They are derived from MenuComponent class. Following hierarchy illustrates.
Java AWT Menu Hierarchy
Menus creation involves lot of classes like MenuBar, Menu and MenuItem and one is added to the other.
MenuComponent is the super most class of all menu classes; like Component is the super most class for all component classes like Button, Frame etc. MenuBar is capable to hold the menus and Menu can hold menu items. Menus are placed on menu bar.
Steps of Creating Java AWT Menu
Creation of menus involves many steps to be followed in an order. Following are the steps.
- Create menu bar
- Add menu bar to the frame
- Create menus
- Add menus to menu bar
- Create menu items
- Add menu items to menus
- Event handling
In the following program, three menus are created and populated with menu items. User's selected menu item information is displayed in the text area.
Example on Java AWT Menu
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class MenuDemo extends Frame implements ActionListener
{
TextArea ta;
public MenuDemo()
{ // create menu bar
MenuBar mBar = new MenuBar();
setMenuBar(mBar); // add menu bar to frame
// create menus
Menu files = new Menu("Files");
Menu date = new Menu("Date");
Menu exit = new Menu("Exit");
ta = new TextArea(10, 40);
ta.setBackground(Color.cyan);
// add menus to menu bar
mBar.add(files);
mBar.add(date);
mBar.add(exit);
// create menu items to menus
MenuItem mi1 = new MenuItem("GLDemo.java");
files.add(mi1); // using anonymous menu items
files.add(new MenuItem("LabelTest.java"));
files.addSeparator();
files.add(new MenuItem("UsingFonts.class"));
files.add(new MenuItem("FLDemo.class"));
date.add(new MenuItem("Today"));
exit.add(new MenuItem("Close"));
// linking listener
files.addActionListener(this);
date.addActionListener(this);
exit.addActionListener(this);
add(ta, "Center");
setTitle("Menu Practice");
setSize(400, 400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
if(str.equals("Close"))
{
System.exit(0);
}
else if(str.equals("Today"))
{
ta.setText("Today: " + new java.util.Date());
}
else
{
try
{
FileReader fr = new FileReader(str);
ta.setText("Folloiwing are file contents:\n\n");
int temp;
while( (temp = fr.read()) != -1)
{
char ch = (char) temp;
String s1 = String.valueOf(ch);
ta.append(s1);
}
fr.close();
}
catch(IOException e1)
{
ta.setText("Exception: " + e1);
}
}
}
public static void main(String args[])
{
new MenuDemo();
}
}
errors are lot in above program
The above code has a lot of errors…..
why the program have a lot of errors?..
this website is very usefull for me to understand java programs