In this step, we will process the menu items. Recall from the earlier discussion that thehandelEvent
method in theComponent
class looks for menu events and, when it finds any, sends them to anaction
method. Thus, we must override the Component class action method.In our
action
method, our strategy is to look for events whose target is aMenuItem
. When we see one of them, we will check the label on the particular menuitem, through theevent.arg
parameter.The following block of code does this; the only "processing" we do, however, is to print out a message identifying the event that was handled. 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.
public boolean action ( Event event, Object object ) { if ( event.target instanceof MenuItem ) { if ( (event.arg).equals("New") ) { System.out.println ( "File Menu: New selected" ); return true; } if ( (event.arg).equals("Open") ) { System.out.println ( "File Menu: Open selected" ); return true; } if ( (event.arg).equals("Close") ) { System.out.println ( "File Menu: Close selected" ); return true; } if ( (event.arg).equals("Save") ) { System.out.println ( "File Menu: Save selected" ); return true; } if ( (event.arg).equals("Quit") ) { System.out.println ( "File Menu: Quit selected" ); return true; } if ( (event.arg).equals("Cut") ) { System.out.println ( "Edit Menu: Cut selected" ); return true; } if ( (event.arg).equals("Copy") ) { System.out.println ( "Edit Menu: Copy selected" ); return true; } if ( (event.arg).equals("Paste") ) { System.out.println ( "Edit Menu: Paste selected" ); return true; } if ( (event.arg).equals("Delete") ) { System.out.println ( "Edit Menu: Delete selected" ); return true; } if ( (event.arg).equals("Help") ) { System.out.println ( "Help Menu: Help selected" ); return true; } } // end menuitem test return false; } // end action
We will delay running the applet until we have also handled scrollbar events..