< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */

  25 package java.awt;
  26 


  27 import java.io.IOException;
  28 import java.io.ObjectInputStream;
  29 import java.util.Vector;
  30 import java.util.Enumeration;






  31 import sun.awt.AWTAccessor;
  32 import java.awt.peer.MenuBarPeer;
  33 import java.awt.event.KeyEvent;
  34 import javax.accessibility.*;
  35 
  36 /**
  37  * The {@code MenuBar} class encapsulates the platform's
  38  * concept of a menu bar bound to a frame. In order to associate
  39  * the menu bar with a {@code Frame} object, call the
  40  * frame's {@code setMenuBar} method.
  41  * <p>
  42  * <A NAME="mbexample"></A><!-- target for cross references -->
  43  * This is what a menu bar might look like:
  44  * <p>
  45  * <img src="doc-files/MenuBar-1.gif"
  46  * alt="Diagram of MenuBar containing 2 menus: Examples and Options.
  47  * Examples menu is expanded showing items: Basic, Simple, Check, and More Examples."
  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.)


  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;
 118 
 119     /**
 120      * Creates a new menu bar.
 121      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 122      * returns true.
 123      * @see java.awt.GraphicsEnvironment#isHeadless
 124      */
 125     public MenuBar() throws HeadlessException {
 126     }
 127 
 128     /**
 129      * Construct a name for this MenuComponent.  Called by getName() when


 235                 menus.addElement(m);
 236             }
 237             return m;
 238         }
 239     }
 240 
 241     /**
 242      * Removes the menu located at the specified
 243      * index from this menu bar.
 244      * @param        index   the position of the menu to be removed.
 245      * @see          java.awt.MenuBar#add(java.awt.Menu)
 246      */
 247     public void remove(final int index) {
 248         synchronized (getTreeLock()) {
 249             Menu m = getMenu(index);
 250             menus.removeElementAt(index);
 251             MenuBarPeer peer = (MenuBarPeer)this.peer;
 252             if (peer != null) {
 253                 peer.delMenu(index);
 254                 m.removeNotify();
 255                 m.parent = null;
 256             }

 257             if (helpMenu == m) {
 258                 helpMenu = null;
 259                 m.isHelpMenu = false;
 260             }
 261         }
 262     }
 263 
 264     /**
 265      * Removes the specified menu component from this menu bar.
 266      * @param        m the menu component to be removed.
 267      * @see          java.awt.MenuBar#add(java.awt.Menu)
 268      */
 269     public void remove(MenuComponent m) {
 270         synchronized (getTreeLock()) {
 271             int index = menus.indexOf(m);
 272             if (index >= 0) {
 273                 remove(index);
 274             }
 275         }
 276     }


   1 /*
   2  * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.event.KeyEvent;
  29 import java.awt.peer.MenuBarPeer;
  30 import java.io.IOException;
  31 import java.io.ObjectInputStream;

  32 import java.util.Enumeration;
  33 import java.util.Vector;
  34 
  35 import javax.accessibility.Accessible;
  36 import javax.accessibility.AccessibleContext;
  37 import javax.accessibility.AccessibleRole;
  38 
  39 import sun.awt.AWTAccessor;



  40 
  41 /**
  42  * The {@code MenuBar} class encapsulates the platform's
  43  * concept of a menu bar bound to a frame. In order to associate
  44  * the menu bar with a {@code Frame} object, call the
  45  * frame's {@code setMenuBar} method.
  46  * <p>
  47  * <A NAME="mbexample"></A><!-- target for cross references -->
  48  * This is what a menu bar might look like:
  49  * <p>
  50  * <img src="doc-files/MenuBar-1.gif"
  51  * alt="Diagram of MenuBar containing 2 menus: Examples and Options.
  52  * Examples menu is expanded showing items: Basic, Simple, Check, and More Examples."
  53  * style="float:center; margin: 7px 10px;">
  54  * <p>
  55  * A menu bar handles keyboard shortcuts for menu items, passing them
  56  * along to its child menus.
  57  * (Keyboard shortcuts, which are optional, provide the user with
  58  * an alternative to the mouse for invoking a menu item and the
  59  * action that is associated with it.)


  82         }
  83         AWTAccessor.setMenuBarAccessor(
  84             new AWTAccessor.MenuBarAccessor() {
  85                 public Menu getHelpMenu(MenuBar menuBar) {
  86                     return menuBar.helpMenu;
  87                 }
  88 
  89                 public Vector<Menu> getMenus(MenuBar menuBar) {
  90                     return menuBar.menus;
  91                 }
  92             });
  93     }
  94 
  95     /**
  96      * This field represents a vector of the
  97      * actual menus that will be part of the MenuBar.
  98      *
  99      * @serial
 100      * @see #countMenus()
 101      */
 102     private final Vector<Menu> menus = new Vector<>();
 103 
 104     /**
 105      * This menu is a special menu dedicated to
 106      * help.  The one thing to note about this menu
 107      * is that on some platforms it appears at the
 108      * right edge of the menubar.
 109      *
 110      * @serial
 111      * @see #getHelpMenu()
 112      * @see #setHelpMenu(Menu)
 113      */
 114     private volatile Menu helpMenu;
 115 
 116     private static final String base = "menubar";
 117     private static int nameCounter = 0;
 118 
 119     /*
 120      * JDK 1.1 serialVersionUID
 121      */
 122      private static final long serialVersionUID = -4930327919388951260L;
 123 
 124     /**
 125      * Creates a new menu bar.
 126      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 127      * returns true.
 128      * @see java.awt.GraphicsEnvironment#isHeadless
 129      */
 130     public MenuBar() throws HeadlessException {
 131     }
 132 
 133     /**
 134      * Construct a name for this MenuComponent.  Called by getName() when


 240                 menus.addElement(m);
 241             }
 242             return m;
 243         }
 244     }
 245 
 246     /**
 247      * Removes the menu located at the specified
 248      * index from this menu bar.
 249      * @param        index   the position of the menu to be removed.
 250      * @see          java.awt.MenuBar#add(java.awt.Menu)
 251      */
 252     public void remove(final int index) {
 253         synchronized (getTreeLock()) {
 254             Menu m = getMenu(index);
 255             menus.removeElementAt(index);
 256             MenuBarPeer peer = (MenuBarPeer)this.peer;
 257             if (peer != null) {
 258                 peer.delMenu(index);
 259                 m.removeNotify();

 260             }
 261             m.parent = null;
 262             if (helpMenu == m) {
 263                 helpMenu = null;
 264                 m.isHelpMenu = false;
 265             }
 266         }
 267     }
 268 
 269     /**
 270      * Removes the specified menu component from this menu bar.
 271      * @param        m the menu component to be removed.
 272      * @see          java.awt.MenuBar#add(java.awt.Menu)
 273      */
 274     public void remove(MenuComponent m) {
 275         synchronized (getTreeLock()) {
 276             int index = menus.indexOf(m);
 277             if (index >= 0) {
 278                 remove(index);
 279             }
 280         }
 281     }


< prev index next >