import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

        // An illustration of JMenuBar, JMenu and associated
        // menu components.
class SwingMenuFrame1 extends JFrame {
    public SwingMenuFrame1() {
        final int width = 400, height = 325;
        setSize(width,height);

        JMenuBar bar = new JMenuBar();
        // Place the menu bar.
        setJMenuBar(bar);
        // Add the first menu.
        JMenu foodMenu = createRestaurantMenu();
        foodMenu.setMnemonic('M');
        bar.add(foodMenu);

        // Add a second menu to quit the application.
        JMenu quit = new JMenu("Quit");
        quit.setMnemonic('Q');
        // Allow a second chance.
        quit.add(new JMenuItem("Cancel"));
        // Confirm quitting.
        Action reallyQuit = new AbstractAction("Really quit"){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        };
        quit.add(reallyQuit);
        bar.add(quit);

        // List the chosen menu items in a central area.
        final int listWidth = 125, listHeight = 200; 
        JScrollPane pane = new JScrollPane(getItemList());
        pane.setPreferredSize(new Dimension(listWidth,listHeight));
        JPanel panel = new JPanel();
        panel.add(pane,"Center");
        Container contents = getContentPane();
        contents.add(panel);
    }

        /* Create a JMenu with a set of restaurant menu items.
         * There are three separate areas for Starters, Main Course
         * and Drinks. The first two are nested JMenus.
         */
    protected JMenu createRestaurantMenu(){
        // The menu, to which all items are ultimately added.
        final JMenu menu = new JMenu("Menu");

        // A sub-menu for the starters.
        final JMenu starters = new JMenu("Starters");
        starters.add(new SelectableMenuItem("Soup"));
        starters.add(new SelectableMenuItem("Melon"));
        starters.add(new SelectableMenuItem("Avocado"));
        menu.add(starters);

        menu.addSeparator();

        // The main course choices.  The beef is off.
        menu.add(new SelectableMenuItem("Quiche"));
        menu.add(new SelectableMenuItem("Pizza"));
        menu.add(new SelectableMenuItem("Turkey"));
        menu.add(new SelectableMenuItem("Salmon"));
        JMenuItem beef = new SelectableMenuItem("Beef");
        beef.setEnabled(false);
        menu.add(beef);
        // The vegetables are in a sub menu.
        menu.add(setupVegetables());

        menu.addSeparator();

        // The drinks are in a sub menu.
        menu.add(setupDrinks());

        return menu;
    }

        // Allow a selection of vegetables. Any number of
        // these may be chosen.
    protected JMenuItem setupVegetables(){
        final String[] choices = {
            "Carrots", "Broccoli", "Peas", "Asparagus", "Brussels Sprouts",
        };
        final int numVegetables = choices.length;

        // Add each item as a check box.
        final JMenu vegetables = new JMenu("Vegetables");
        for(int i = 0; i < numVegetables; i++){
            final JCheckBoxMenuItem item = new JCheckBoxMenuItem(choices[i]);
            // Listen for selection or deselection and add or remove
            // the item from the application's list.
            item.addItemListener(new ItemListener(){
                public void itemStateChanged(ItemEvent e){
                    int change = e.getStateChange();
                    // Get the data items for the list.
                    final DefaultListModel model =
                            (DefaultListModel) getItemList().getModel();
                    if(change == ItemEvent.SELECTED){
                        model.addElement(item.getText());
                    }
                    else{
                        model.removeElement(item.getText());
                    }
                }
            });
            vegetables.add(item);
        }
        return vegetables;
    }

        // Return a menu item for the drinks.
        // Provide a colored icon for each.
    protected JMenuItem setupDrinks(){
        Object[][] choices = {
            {"Apple", Color.green},
            {"Red Grape", Color.red},
            {"White Grape", Color.white},
        };
        final int numDrinks = choices.length;

        final JMenu drinks = new JMenu("Drinks");
        for(int i = 0; i < numDrinks; i++){
            final JMenuItem item = new SelectableMenuItem(
                                (String) choices[i][0],
                                new CircleIcon((Color) choices[i][1]));
            drinks.add(item);
        }
        return drinks;
    }

        /* A JMenuItem that adds its associated text to the
         * central JList when it is selected.
         * By implementing ActionListener, the item can respond
         * to its selection by adding its text to the list.
         */
    protected class SelectableMenuItem extends JMenuItem
                                        implements ActionListener {
        public SelectableMenuItem(String item,Icon icon){
            super(item,icon);
            addActionListener(this);
            // Position the text to the right of the icon.
            setHorizontalTextPosition(JMenuItem.RIGHT);
        }

        public SelectableMenuItem(String item){
            super(item);
            addActionListener(this);
        }

        // Add the text of this item to the list when selected.
        public void actionPerformed(ActionEvent e){
            JMenuItem item = (JMenuItem) e.getSource();
            // Get the data items for the list.
            final DefaultListModel model =
                    (DefaultListModel) getItemList().getModel();
            model.addElement(item.getText());
        }
    }

    protected JList getItemList(){
        return itemList;
    }

    // Which items have been selected from the menu.
    private JList itemList = new JList(new DefaultListModel());
}
