Home Segments Top Top Previous Next

953: Mainline

You could set up separate listeners for both the fileMenuGeneral and fileMenuHorror menu items. Alternatively, you can define one listener and sort out in that listener which menu item has been clicked. Most programmers would define just one listener, on the ground that it is best to keep together all the responses to menu clicks. To determine which menu item has been clicked, the listener uses the getSource method with the event as the argument, as shown in the following preview of the definition of the listener, which is to be an inner class of the MovieMenuBar class:

class LocalActionListener implements ActionListener {                  
 public void actionPerformed (ActionEvent e) {                         
  JMenuItem jMenuItem = (JMenuItem)(e.getSource()); 
  if (jMenuItem == fileMenuGeneral) { 
   applet.getMovieData().setMovieVector( 
    MovieAuxiliaries.readMovieFile("general.movies") 
   ); 
  } 
  else if (jMenuItem == fileMenuHorror) { 
   applet.getMovieData().setMovieVector( 
    MovieAuxiliaries.readMovieFile("horror.movies") 
   ); 
}}}