import java.awt.event.*;
import javax.swing.*;

public class MenuExample extends JFrame implements ActionListener {
	
    // Store menu items and popup menu for access from event handlers
    JMenuItem   thinkItem, copyItem, newItem, openItem, saveAsItem,
                findItem, replaceItem, appleItem, orangeItem,
                bannanaItem, helpItem, inspectItem;

    JPopupMenu popupMenu;
    

    public MenuExample(String title) {
        super(title);
        
        // Create the menu bar
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        // Create and Add the File menu to the Menu Bar
        JMenu  fileMenu = new JMenu("File");
        fileMenu.setMnemonic('F');
        fileMenu.add(newItem = new JMenuItem("New", 'N'));
        fileMenu.add(new JSeparator());
        fileMenu.add(openItem = new JMenuItem("Open", 'O'));
        fileMenu.add(saveAsItem = new JMenuItem("Save As"));
        menuBar.add(fileMenu); // Don't forget to do this
        newItem.addActionListener(this);
        openItem.addActionListener(this);
        saveAsItem.addActionListener(this);

         
        // Create and Add the Edit menu to the Menu Bar
        JMenu  editMenu = new JMenu("Edit");
        editMenu.setMnemonic('E');
        editMenu.add(thinkItem = new JMenuItem("Think", new ImageIcon("brain.gif")));
        editMenu.add(copyItem = new JMenuItem("Copy"));
        menuBar.add(editMenu);
        thinkItem.addActionListener(this);
        copyItem.addActionListener(this);
         
        // Create and Add the Settings menu to the Menu Bar
        JMenu  settingsMenu = new JMenu("Settings");
        settingsMenu.setMnemonic('S');
        settingsMenu.add(appleItem = new JRadioButtonMenuItem("Apples"));
        settingsMenu.add(orangeItem = new JRadioButtonMenuItem("Oranges"));
        settingsMenu.add(bannanaItem = new JRadioButtonMenuItem("Bannanas"));
        menuBar.add(settingsMenu);

        // Ensure that only one radio button is on at a time
        ButtonGroup  fruits = new ButtonGroup();
        fruits.add(appleItem);
        fruits.add(orangeItem);
        fruits.add(bannanaItem);

        // Create the cascading Search menu on the Settings menu
        JMenu  searchMenu = new JMenu("Search");
        searchMenu.add(findItem = new JMenuItem("Find"));
        searchMenu.add(replaceItem = new JMenuItem("Replace"));
        editMenu.add(searchMenu);
        findItem.addActionListener(this);
        replaceItem.addActionListener(this);

        // Create and Add items to the popup menu.   Notice
        // that we do not add the popup menu to anything.
        popupMenu = new JPopupMenu();
        popupMenu.add(helpItem = new JMenuItem("help"));
        popupMenu.add(inspectItem = new JMenuItem("inspect"));
        helpItem.addActionListener(this);
        inspectItem.addActionListener(this);

        // Register the event handler for the popup menu
        addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e){
                if (e.isPopupTrigger())
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        });
        
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 300);

    }

    // Handle all menu selections and dispatch to the appripriate helper method accordingly
    public void actionPerformed(ActionEvent e){

        if (e.getSource() == newItem)
            reactToNewMenuSelection();
        else if (e.getSource() == openItem)
            reactToOpenMenuSelection();
        else if (e.getSource() == saveAsItem)
            reactToSaveAsMenuSelection();
        else if (e.getSource() == copyItem)
            reactToCopyMenuSelection();
        else if (e.getSource() == thinkItem)
            reactToThinkMenuSelection();
        else if (e.getSource() == findItem)
            reactToFindMenuSelection();
        else if (e.getSource() == replaceItem)
            reactToReplaceMenuSelection();
        else if (e.getSource() == helpItem)
            reactToHelpMenuSelection();
        else if (e.getSource() == inspectItem)
            reactToInspectMenuSelection();

    }
    // Here are all the helper methods for handling the menu choices
    public void reactToNewMenuSelection() {
        System.out.println("reacting to NEW selection from menu");
    }
    public void reactToOpenMenuSelection() {
        System.out.println("reacting to OPEN selection from menu");
    }
    public void reactToSaveAsMenuSelection() {
        System.out.println("reacting to SAVE AS selection from menu");
    }
    public void reactToThinkMenuSelection() {
        System.out.println("reacting to THINK selection from menu");
    }
    public void reactToCopyMenuSelection() {
        System.out.println("reacting to COPY selection from menu");
    }
    public void reactToFindMenuSelection() {
        System.out.println("reacting to FIND selection from menu");
    }
    public void reactToReplaceMenuSelection() {
        System.out.println("reacting to REPLACE selection from menu");
    }
    public void reactToHelpMenuSelection() {
        System.out.println("reacting to HELP selection from popup menu");
    }
    public void reactToInspectMenuSelection() {
        System.out.println("reacting to INSPECT selection from popup menu");
    }
    public static void main(String args[]) {
        new MenuExample("Menu Example").setVisible(true);
    }
}