You can borrow nearly all you need for a popup menu from what you need for
a menu bar. In particular, you can retain the fileMenuGeneral
and
fileMenuHorror
menu items.
Now, however, instead of creating a subclass of the JMenu
class, you
create a subclass of the JPopupMenu
class. You no longer need a
JMenu
instance variable; the JMenuItem
instances connect
directly to the MoviePopupMenu
instance.
import java.awt.*; import java.util.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class MoviePopupMenu extends JPopupMenu { MovieApplication applet; private JMenuItem fileMenuGeneral = new JMenuItem("General"); private JMenuItem fileMenuHorror = new JMenuItem("Horror"); public MoviePopupMenu (MovieApplication a) { applet = a; add(fileMenuGeneral); 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") ); }}}}