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

Print this page
rev 10048 : 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
Reviewed-by:


  48  * style="float:center; margin: 7px 10px;">
  49  * <p>
  50  * A menu bar handles keyboard shortcuts for menu items, passing them
  51  * along to its child menus.
  52  * (Keyboard shortcuts, which are optional, provide the user with
  53  * an alternative to the mouse for invoking a menu item and the
  54  * action that is associated with it.)
  55  * Each menu item can maintain an instance of <code>MenuShortcut</code>.
  56  * The <code>MenuBar</code> class defines several methods,
  57  * {@link MenuBar#shortcuts} and
  58  * {@link MenuBar#getShortcutMenuItem}
  59  * that retrieve information about the shortcuts a given
  60  * menu bar is managing.
  61  *
  62  * @author Sami Shaio
  63  * @see        java.awt.Frame
  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     }


 255         }
 256     }
 257 
 258     /**
 259      * Removes the specified menu component from this menu bar.
 260      * @param        m the menu component to be removed.
 261      * @see          java.awt.MenuBar#add(java.awt.Menu)
 262      */
 263     public void remove(MenuComponent m) {
 264         synchronized (getTreeLock()) {
 265             int index = menus.indexOf(m);
 266             if (index >= 0) {
 267                 remove(index);
 268             }
 269         }
 270     }
 271 
 272     /**
 273      * Gets the number of menus on the menu bar.
 274      * @return     the number of menus on the menu bar.
 275      * @since      JDK1.1
 276      */
 277     public int getMenuCount() {
 278         return countMenus();
 279     }
 280 
 281     /**
 282      * @deprecated As of JDK version 1.1,
 283      * replaced by <code>getMenuCount()</code>.
 284      */
 285     @Deprecated
 286     public int countMenus() {
 287         return getMenuCountImpl();
 288     }
 289 
 290     /*
 291      * This is called by the native code, so client code can't
 292      * be called on the toolkit thread.
 293      */
 294     final int getMenuCountImpl() {
 295         return menus.size();


 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();
 348         for (int i = 0 ; i < nmenus ; i++) {
 349             MenuItem mi = getMenu(i).getShortcutMenuItem(s);
 350             if (mi != null) {
 351                 return mi;
 352             }
 353         }
 354         return null;  // MenuShortcut wasn't found
 355      }
 356 
 357     /*
 358      * Post an ACTION_EVENT to the target of the MenuPeer
 359      * associated with the specified keyboard event (on
 360      * keydown).  Returns true if there is an associated
 361      * keyboard event.
 362      */
 363     boolean handleShortcut(KeyEvent e) {
 364         // Is it a key event?


 370         // Is the accelerator modifier key pressed?
 371         int accelKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
 372         if ((e.getModifiers() & accelKey) == 0) {
 373             return false;
 374         }
 375 
 376         // Pass MenuShortcut on to child menus.
 377         int nmenus = getMenuCount();
 378         for (int i = 0 ; i < nmenus ; i++) {
 379             Menu m = getMenu(i);
 380             if (m.handleShortcut(e)) {
 381                 return true;
 382             }
 383         }
 384         return false;
 385     }
 386 
 387     /**
 388      * Deletes the specified menu shortcut.
 389      * @param     s the menu shortcut to delete.
 390      * @since     JDK1.1
 391      */
 392     public void deleteShortcut(MenuShortcut s) {
 393         int nmenus = getMenuCount();
 394         for (int i = 0 ; i < nmenus ; i++) {
 395             getMenu(i).deleteShortcut(s);
 396         }
 397     }
 398 
 399     /* Serialization support.  Restore the (transient) parent
 400      * fields of Menubar menus here.
 401      */
 402 
 403     /**
 404      * The MenuBar's serialized data version.
 405      *
 406      * @serial
 407      */
 408     private int menuBarSerializedDataVersion = 1;
 409 
 410     /**




  48  * style="float:center; margin: 7px 10px;">
  49  * <p>
  50  * A menu bar handles keyboard shortcuts for menu items, passing them
  51  * along to its child menus.
  52  * (Keyboard shortcuts, which are optional, provide the user with
  53  * an alternative to the mouse for invoking a menu item and the
  54  * action that is associated with it.)
  55  * Each menu item can maintain an instance of <code>MenuShortcut</code>.
  56  * The <code>MenuBar</code> class defines several methods,
  57  * {@link MenuBar#shortcuts} and
  58  * {@link MenuBar#getShortcutMenuItem}
  59  * that retrieve information about the shortcuts a given
  60  * menu bar is managing.
  61  *
  62  * @author Sami Shaio
  63  * @see        java.awt.Frame
  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      1.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     }


 255         }
 256     }
 257 
 258     /**
 259      * Removes the specified menu component from this menu bar.
 260      * @param        m the menu component to be removed.
 261      * @see          java.awt.MenuBar#add(java.awt.Menu)
 262      */
 263     public void remove(MenuComponent m) {
 264         synchronized (getTreeLock()) {
 265             int index = menus.indexOf(m);
 266             if (index >= 0) {
 267                 remove(index);
 268             }
 269         }
 270     }
 271 
 272     /**
 273      * Gets the number of menus on the menu bar.
 274      * @return     the number of menus on the menu bar.
 275      * @since      1.1
 276      */
 277     public int getMenuCount() {
 278         return countMenus();
 279     }
 280 
 281     /**
 282      * @deprecated As of JDK version 1.1,
 283      * replaced by <code>getMenuCount()</code>.
 284      */
 285     @Deprecated
 286     public int countMenus() {
 287         return getMenuCountImpl();
 288     }
 289 
 290     /*
 291      * This is called by the native code, so client code can't
 292      * be called on the toolkit thread.
 293      */
 294     final int getMenuCountImpl() {
 295         return menus.size();


 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       1.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        1.1
 345      */
 346      public MenuItem getShortcutMenuItem(MenuShortcut s) {
 347         int nmenus = getMenuCount();
 348         for (int i = 0 ; i < nmenus ; i++) {
 349             MenuItem mi = getMenu(i).getShortcutMenuItem(s);
 350             if (mi != null) {
 351                 return mi;
 352             }
 353         }
 354         return null;  // MenuShortcut wasn't found
 355      }
 356 
 357     /*
 358      * Post an ACTION_EVENT to the target of the MenuPeer
 359      * associated with the specified keyboard event (on
 360      * keydown).  Returns true if there is an associated
 361      * keyboard event.
 362      */
 363     boolean handleShortcut(KeyEvent e) {
 364         // Is it a key event?


 370         // Is the accelerator modifier key pressed?
 371         int accelKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
 372         if ((e.getModifiers() & accelKey) == 0) {
 373             return false;
 374         }
 375 
 376         // Pass MenuShortcut on to child menus.
 377         int nmenus = getMenuCount();
 378         for (int i = 0 ; i < nmenus ; i++) {
 379             Menu m = getMenu(i);
 380             if (m.handleShortcut(e)) {
 381                 return true;
 382             }
 383         }
 384         return false;
 385     }
 386 
 387     /**
 388      * Deletes the specified menu shortcut.
 389      * @param     s the menu shortcut to delete.
 390      * @since     1.1
 391      */
 392     public void deleteShortcut(MenuShortcut s) {
 393         int nmenus = getMenuCount();
 394         for (int i = 0 ; i < nmenus ; i++) {
 395             getMenu(i).deleteShortcut(s);
 396         }
 397     }
 398 
 399     /* Serialization support.  Restore the (transient) parent
 400      * fields of Menubar menus here.
 401      */
 402 
 403     /**
 404      * The MenuBar's serialized data version.
 405      *
 406      * @serial
 407      */
 408     private int menuBarSerializedDataVersion = 1;
 409 
 410     /**