/*
 *  ==============================================================
 *  DemoMenuBar.java : Create bar of menu items -- file, edit,
 *  format, options. Each menu item has a small red-dot gif image.
 * 
 *  Adapted from : Pantham S., Pure JFC Swing, 1999.
 *  Modified by : Mark Austin                          March, 2001
 *  ==============================================================
 */
  
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.net.URL;
 
public class DemoMenuBar extends JApplet {

    URL codeBase;   // used for browser version only....
    URL   redURL;   // URL for red image ...

    public void init() {

        // 1. Get the handle on the applet's content pane.

        Container container = this.getContentPane();

        // 2. Construct URL for image....

        codeBase = getCodeBase();
        try {
           redURL = new URL( codeBase, "reddot.gif");
        } catch ( java.net.MalformedURLException e ) {
           System.out.println("Badly specified URL!!");
        }

        // 3. Create a menu bar with bevel border and
        //    add it to the applet.

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBorder( new BevelBorder(BevelBorder.RAISED) );
        container.add(menuBar, BorderLayout.NORTH);

        // 4. Create menus for a simple editor.

        JMenu fileMenu    = new JMenu("File", true);
        JMenu editMenu    = new JMenu("Edit");
        JMenu formatMenu  = new JMenu("Format");
        JMenu optionsMenu = new JMenu("Options");
        
        // 5. Add the menus to the menu bar.

        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(formatMenu);
        menuBar.add(optionsMenu);
                              
        // 6. Get the handle on each menu to the normal and
        //    selected icons and the mnemonic (short-cut) key.

        for (int i=0; i<menuBar.getMenuCount(); i++) {
           JMenu menu = menuBar.getMenu(i);  // returns the menu

           menu.setIcon( new ImageIcon( redURL ) );
           
           // Set mnemonic key.

           String text = menu.getText();     // gets the menu label
           menu.setMnemonic(text.charAt(0)); // at first char
        }
    }
}
