Next, you attach a
LocalActionListener
instance to the appropriate menu items in the
MovieApplet
initialization method, using
addActionListener
, as shown in the following definition, which
brings together all the previously described program fragments:
import java.awt.*; import java.util.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class MovieMenuBar extends JMenuBar { MovieApplication applet; private JMenu menu = new JMenu("File"); private JMenuItem fileMenuGeneral = new JMenuItem("General"); private JMenuItem fileMenuHorror = new JMenuItem("Horror"); public MovieMenuBar (MovieApplication a) { applet = a; add(menu); menu.add(fileMenuGeneral); menu.add(fileMenuHorror); LocalActionListener listener = new LocalActionListener(); fileMenuGeneral.addActionListener(listener); fileMenuHorror.addActionListener(listener); } 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") ); }}}}