Step 2


Overview

In this step, you will add a "File" menu to the application. At this time, it only contains an "Exit" menu item. To create a menu, you first create a new kind of object: an Action object. The action object is added to a menu. Then the menu is added to a menu bar and the menu bar is installed into the top-level window of the application. All of these changes are made in the constructor for the FileViewer class.

You will be given all of the code for this step. Although you could go through the step quickly by just adding the code sequentially at the end of the constructor, it is better to proceed slowly. Read through the explanations carefully. You will be adding two more action objects in the next two steps, and adding them to the "File" menu. This step will be used as a model.

Creating an Action for Exiting the Program

Action objects are action listeners that have a table of properties that is used to determine how they are treated in a menu or a toolbar. In addition to an actionPerformed() method, they have a putValue() method for setting a property and a getValue() method for retrieving a property. You, as a programmer, invoke putValue(). Menus and toolbars invoke getValue(). This gives you the capability of telling menus and toolbars what they should do with an action.

There is an AbstractAction class that implements all of the Action interface methods except the actionPerformed() method. Here is the code to create your exit action.

    Action exitAction = new AbstractAction() {
	public void actionPerformed(ActionEvent e) {
	    exit();
	}
    };
    exitAction.putValue(Action.NAME, "Exit");
In this code, you have defined the actionPerformed() method for the new action to just invoke the exit() method of the FileViewer class. The putValue() message gives a name to the action. When this action is added to a menu, the menu uses the name property as the label on the menu item that it creates.

Creating a "File" Menu Containing the "Exit" Menu Item

To create the "File" menu, you declare a JMenu variable, initialized by invoking the JMenu constructor. A constructor parameter specifies the name of the menu. Menu items are added to the menu by sending the menu an add() message. When you are using actions, you use an action as an add() parameter. Here is the code.
    JMenu fileMenu = new JMenu("File");
    fileMenu.add(exitAction);
When the file menu receives the add() message, it creates a menu item (class JMenuItem) for the action, using the Action getValue() method to determine the label for the menu item. Then it adds the menu item into the menu.

Creating and Installing a Menu Bar

Menus must be added to a menu bar, which must be installed into an applet or application. Here is the code for doing this.
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    setJmenuBar(menuBar);
The first statement creates the menu bar. The second adds the file menu to the menu bar. The third installs the menu bar into the application top-level window.

Although you have not done so, you can add menu bars and menus to applets using the same code as shown here.

That is all of the code for this step. When you have completed the changes, recompile the FileViewer class and run it again by giving the following command.

    java FileViewer
You should see a "File" menu near the top of the application. When you click on it, its "Exit" menu item should pop up. When you click on the "Exit" menu item, the program should terminate.