Step 4

In this step, we will process the menu items. Recall from the earlier discussion that the MenuItem class receives the events initially. In the code, below, we will elect to process those events in the applet, itself. Thus, our applet class must implement the ActionListener interface, and we will add self as the recipient object for each menuitem action.

In our actionPerformed method -- the only method included in the ActionListener interface -- our strategy will be to look for events whose source is an instance of the MenuItem class. When we see one of them, we will check the particular object that generated the event to determine which menuitem generated the event.

The following block of code does this; the only "processing" we do, however, is to print out a message identifying the menuitem that was selected. In an real application, your program would include code to actually process the event. This is usually done through a call to a method elsewhere in the program.

In the program, below, the buildWindow method has been omitted to make the code more readable.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class step4 extends Applet implements ActionListener  {

// adds scrollbars and message display area

MyFrame outerBox;

Panel centerPanel;
Panel bottomPanel;

  public void init ( ){
    buildWindow ();
  } // end init

  //****************** buildWindow

    // *** Global variables for ui components

    MenuBar menuBar;

    Menu fileMenu;
      MenuItem fileNew, fileOpen, fileClose, fileSave, fileQuit;

    Menu editMenu;
      MenuItem editCut, editCopy, editPaste, editDelete;

    Menu helpMenu;
      MenuItem helpHelp;

    Scrollbar scrollbarV;
    Scrollbar scrollbarH;

    TextField message;


  //***********  buildWindow method ommitted here   ***********


  //***********  Interface Methods   ***********

  //****  ActionListener methods

  public void actionPerformed ( ActionEvent e )  {

    Object s = e.getSource();

    // *** process Menu actions

    if ( s instanceof MenuItem )  {

        // *** file menu items
        if ( s == fileNew )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileOpen )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileClose )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileSave )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == fileQuit )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }

        // *** edit menu items
        if ( s == editCut )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == editCopy )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == editPaste )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }
        if ( s == editDelete )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }

        // *** help menu items
        if ( s == helpHelp )  {
            System.out.println ( "MenuItem "+e.getActionCommand()+" detected" );
        }

    }  // end process menu actions

  }  // end actionPerformed


}// end step4



We will delay running the applet until we have also handled scrollbar events.