![]() |
![]() |
![]() |
![]() |
![]() |
|
JMenuBar class then create instances of the
JMenu and JMenuItem classes by
instantiating the following patterns:
JMenu menu = new JMenu("label");
JMenuItem menu item = new JMenuItem("label");
then connect together the menu bar, menus, and menu items by instantiating the following pattern:
menu or menu bar.add(menu or menu item);
and then instantiate the following pattern:
setJMenuBar(menu bar);
JMenuBar class:
class LocalActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JMenuItem jMenuItem = (JMenuItem)(e.getSource());
// Determine item clicked and react here
}
}
and then add the listener to the menu item:
menu item.addActionListener(new LocalActionListener());
JPopupMenu class, and then connect menu items to the popup menu
by instantiating the following pattern:
popup menu.add(menu item);
JPopupMenu class:
class LocalActionListener implements ActionListener {
public void actionPerformed (ActionEvent e) {
JMenuItem jMenuItem = (JMenuItem)(e.getSource());
// Determine item clicked and react here
}
}
class LocalMouseListener extends MouseAdapter {
parent class parent;
LocalMouseListener(parent class p) {parent = p;}
public void mouseClicked (MouseEvent e) {
// React to click here
}
}
and then add the listener to the appropriate component:
component.addMouseListener(new LocalMouseListener(this));