src/share/classes/java/awt/MenuBar.java

Print this page




  64  * @see        java.awt.Frame#setMenuBar(java.awt.MenuBar)
  65  * @see        java.awt.Menu
  66  * @see        java.awt.MenuItem
  67  * @see        java.awt.MenuShortcut
  68  * @since      JDK1.0
  69  */
  70 public class MenuBar extends MenuComponent implements MenuContainer, Accessible {
  71 
  72     static {
  73         /* ensure that the necessary native libraries are loaded */
  74         Toolkit.loadLibraries();
  75         if (!GraphicsEnvironment.isHeadless()) {
  76             initIDs();
  77         }
  78         AWTAccessor.setMenuBarAccessor(
  79             new AWTAccessor.MenuBarAccessor() {
  80                 public Menu getHelpMenu(MenuBar menuBar) {
  81                     return menuBar.helpMenu;
  82                 }
  83 
  84                 public Vector getMenus(MenuBar menuBar) {
  85                     return menuBar.menus;
  86                 }
  87             });
  88     }
  89 
  90     /**
  91      * This field represents a vector of the
  92      * actual menus that will be part of the MenuBar.
  93      *
  94      * @serial
  95      * @see #countMenus()
  96      */
  97     Vector menus = new Vector();
  98 
  99     /**
 100      * This menu is a special menu dedicated to
 101      * help.  The one thing to note about this menu
 102      * is that on some platforms it appears at the
 103      * right edge of the menubar.
 104      *
 105      * @serial
 106      * @see #getHelpMenu()
 107      * @see #setHelpMenu(Menu)
 108      */
 109     Menu helpMenu;
 110 
 111     private static final String base = "menubar";
 112     private static int nameCounter = 0;
 113 
 114     /*
 115      * JDK 1.1 serialVersionUID
 116      */
 117      private static final long serialVersionUID = -4930327919388951260L;


 292      * be called on the toolkit thread.
 293      */
 294     final int getMenuCountImpl() {
 295         return menus.size();
 296     }
 297 
 298     /**
 299      * Gets the specified menu.
 300      * @param      i the index position of the menu to be returned.
 301      * @return     the menu at the specified index of this menu bar.
 302      */
 303     public Menu getMenu(int i) {
 304         return getMenuImpl(i);
 305     }
 306 
 307     /*
 308      * This is called by the native code, so client code can't
 309      * be called on the toolkit thread.
 310      */
 311     final Menu getMenuImpl(int i) {
 312         return (Menu)menus.elementAt(i);
 313     }
 314 
 315     /**
 316      * Gets an enumeration of all menu shortcuts this menu bar
 317      * is managing.
 318      * @return      an enumeration of menu shortcuts that this
 319      *                      menu bar is managing.
 320      * @see         java.awt.MenuShortcut
 321      * @since       JDK1.1
 322      */
 323     public synchronized Enumeration<MenuShortcut> shortcuts() {
 324         Vector shortcuts = new Vector();
 325         int nmenus = getMenuCount();
 326         for (int i = 0 ; i < nmenus ; i++) {
 327             Enumeration e = getMenu(i).shortcuts();
 328             while (e.hasMoreElements()) {
 329                 shortcuts.addElement(e.nextElement());
 330             }
 331         }
 332         return shortcuts.elements();
 333     }
 334 
 335     /**
 336      * Gets the instance of <code>MenuItem</code> associated
 337      * with the specified <code>MenuShortcut</code> object,
 338      * or <code>null</code> if none of the menu items being managed
 339      * by this menu bar is associated with the specified menu
 340      * shortcut.
 341      * @param        s the specified menu shortcut.
 342      * @see          java.awt.MenuItem
 343      * @see          java.awt.MenuShortcut
 344      * @since        JDK1.1
 345      */
 346      public MenuItem getShortcutMenuItem(MenuShortcut s) {
 347         int nmenus = getMenuCount();


 421       s.defaultWriteObject();
 422     }
 423 
 424     /**
 425      * Reads the <code>ObjectInputStream</code>.
 426      * Unrecognized keys or values will be ignored.
 427      *
 428      * @param s the <code>ObjectInputStream</code> to read
 429      * @exception HeadlessException if
 430      *   <code>GraphicsEnvironment.isHeadless</code> returns
 431      *   <code>true</code>
 432      * @see java.awt.GraphicsEnvironment#isHeadless
 433      * @see #writeObject(java.io.ObjectOutputStream)
 434      */
 435     private void readObject(ObjectInputStream s)
 436       throws ClassNotFoundException, IOException, HeadlessException
 437     {
 438       // HeadlessException will be thrown from MenuComponent's readObject
 439       s.defaultReadObject();
 440       for (int i = 0; i < menus.size(); i++) {
 441         Menu m = (Menu)menus.elementAt(i);
 442         m.parent = this;
 443       }
 444     }
 445 
 446     /**
 447      * Initialize JNI field and method IDs
 448      */
 449     private static native void initIDs();
 450 
 451 
 452 /////////////////
 453 // Accessibility support
 454 ////////////////
 455 
 456     /**
 457      * Gets the AccessibleContext associated with this MenuBar.
 458      * For menu bars, the AccessibleContext takes the form of an
 459      * AccessibleAWTMenuBar.
 460      * A new AccessibleAWTMenuBar instance is created if necessary.
 461      *




  64  * @see        java.awt.Frame#setMenuBar(java.awt.MenuBar)
  65  * @see        java.awt.Menu
  66  * @see        java.awt.MenuItem
  67  * @see        java.awt.MenuShortcut
  68  * @since      JDK1.0
  69  */
  70 public class MenuBar extends MenuComponent implements MenuContainer, Accessible {
  71 
  72     static {
  73         /* ensure that the necessary native libraries are loaded */
  74         Toolkit.loadLibraries();
  75         if (!GraphicsEnvironment.isHeadless()) {
  76             initIDs();
  77         }
  78         AWTAccessor.setMenuBarAccessor(
  79             new AWTAccessor.MenuBarAccessor() {
  80                 public Menu getHelpMenu(MenuBar menuBar) {
  81                     return menuBar.helpMenu;
  82                 }
  83 
  84                 public Vector<Menu> getMenus(MenuBar menuBar) {
  85                     return menuBar.menus;
  86                 }
  87             });
  88     }
  89 
  90     /**
  91      * This field represents a vector of the
  92      * actual menus that will be part of the MenuBar.
  93      *
  94      * @serial
  95      * @see #countMenus()
  96      */
  97     Vector<Menu> menus = new Vector<>();
  98 
  99     /**
 100      * This menu is a special menu dedicated to
 101      * help.  The one thing to note about this menu
 102      * is that on some platforms it appears at the
 103      * right edge of the menubar.
 104      *
 105      * @serial
 106      * @see #getHelpMenu()
 107      * @see #setHelpMenu(Menu)
 108      */
 109     Menu helpMenu;
 110 
 111     private static final String base = "menubar";
 112     private static int nameCounter = 0;
 113 
 114     /*
 115      * JDK 1.1 serialVersionUID
 116      */
 117      private static final long serialVersionUID = -4930327919388951260L;


 292      * be called on the toolkit thread.
 293      */
 294     final int getMenuCountImpl() {
 295         return menus.size();
 296     }
 297 
 298     /**
 299      * Gets the specified menu.
 300      * @param      i the index position of the menu to be returned.
 301      * @return     the menu at the specified index of this menu bar.
 302      */
 303     public Menu getMenu(int i) {
 304         return getMenuImpl(i);
 305     }
 306 
 307     /*
 308      * This is called by the native code, so client code can't
 309      * be called on the toolkit thread.
 310      */
 311     final Menu getMenuImpl(int i) {
 312         return menus.elementAt(i);
 313     }
 314 
 315     /**
 316      * Gets an enumeration of all menu shortcuts this menu bar
 317      * is managing.
 318      * @return      an enumeration of menu shortcuts that this
 319      *                      menu bar is managing.
 320      * @see         java.awt.MenuShortcut
 321      * @since       JDK1.1
 322      */
 323     public synchronized Enumeration<MenuShortcut> shortcuts() {
 324         Vector<MenuShortcut> shortcuts = new Vector<>();
 325         int nmenus = getMenuCount();
 326         for (int i = 0 ; i < nmenus ; i++) {
 327             Enumeration<MenuShortcut> e = getMenu(i).shortcuts();
 328             while (e.hasMoreElements()) {
 329                 shortcuts.addElement(e.nextElement());
 330             }
 331         }
 332         return shortcuts.elements();
 333     }
 334 
 335     /**
 336      * Gets the instance of <code>MenuItem</code> associated
 337      * with the specified <code>MenuShortcut</code> object,
 338      * or <code>null</code> if none of the menu items being managed
 339      * by this menu bar is associated with the specified menu
 340      * shortcut.
 341      * @param        s the specified menu shortcut.
 342      * @see          java.awt.MenuItem
 343      * @see          java.awt.MenuShortcut
 344      * @since        JDK1.1
 345      */
 346      public MenuItem getShortcutMenuItem(MenuShortcut s) {
 347         int nmenus = getMenuCount();


 421       s.defaultWriteObject();
 422     }
 423 
 424     /**
 425      * Reads the <code>ObjectInputStream</code>.
 426      * Unrecognized keys or values will be ignored.
 427      *
 428      * @param s the <code>ObjectInputStream</code> to read
 429      * @exception HeadlessException if
 430      *   <code>GraphicsEnvironment.isHeadless</code> returns
 431      *   <code>true</code>
 432      * @see java.awt.GraphicsEnvironment#isHeadless
 433      * @see #writeObject(java.io.ObjectOutputStream)
 434      */
 435     private void readObject(ObjectInputStream s)
 436       throws ClassNotFoundException, IOException, HeadlessException
 437     {
 438       // HeadlessException will be thrown from MenuComponent's readObject
 439       s.defaultReadObject();
 440       for (int i = 0; i < menus.size(); i++) {
 441         Menu m = menus.elementAt(i);
 442         m.parent = this;
 443       }
 444     }
 445 
 446     /**
 447      * Initialize JNI field and method IDs
 448      */
 449     private static native void initIDs();
 450 
 451 
 452 /////////////////
 453 // Accessibility support
 454 ////////////////
 455 
 456     /**
 457      * Gets the AccessibleContext associated with this MenuBar.
 458      * For menu bars, the AccessibleContext takes the form of an
 459      * AccessibleAWTMenuBar.
 460      * A new AccessibleAWTMenuBar instance is created if necessary.
 461      *