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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2014, 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


 164                 getMenu(i).removeNotify();
 165             }
 166             super.removeNotify();
 167         }
 168     }
 169 
 170     /**
 171      * Gets the help menu on the menu bar.
 172      * @return    the help menu on this menu bar.
 173      */
 174     public Menu getHelpMenu() {
 175         return helpMenu;
 176     }
 177 
 178     /**
 179      * Sets the specified menu to be this menu bar's help menu.
 180      * If this menu bar has an existing help menu, the old help menu is
 181      * removed from the menu bar, and replaced with the specified menu.
 182      * @param m    the menu to be set as the help menu
 183      */
 184     public void setHelpMenu(Menu m) {
 185         synchronized (getTreeLock()) {
 186             if (helpMenu == m) {
 187                 return;
 188             }
 189             if (helpMenu != null) {
 190                 remove(helpMenu);
 191             }


 192             if (m.parent != this) {
 193                 add(m);
 194             }
 195             helpMenu = m;
 196             if (m != null) {
 197                 m.isHelpMenu = true;
 198                 m.parent = this;
 199                 MenuBarPeer peer = (MenuBarPeer)this.peer;
 200                 if (peer != null) {
 201                     if (m.peer == null) {
 202                         m.addNotify();
 203                     }
 204                     peer.addHelpMenu(m);
 205                 }
 206             }
 207         }
 208     }
 209 
 210     /**
 211      * Adds the specified menu to the menu bar.
 212      * If the menu has been part of another menu bar,
 213      * removes it from that menu bar.
 214      *
 215      * @param        m   the menu to be added
 216      * @return       the menu added


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




 254             }
 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.


   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


 164                 getMenu(i).removeNotify();
 165             }
 166             super.removeNotify();
 167         }
 168     }
 169 
 170     /**
 171      * Gets the help menu on the menu bar.
 172      * @return    the help menu on this menu bar.
 173      */
 174     public Menu getHelpMenu() {
 175         return helpMenu;
 176     }
 177 
 178     /**
 179      * Sets the specified menu to be this menu bar's help menu.
 180      * If this menu bar has an existing help menu, the old help menu is
 181      * removed from the menu bar, and replaced with the specified menu.
 182      * @param m    the menu to be set as the help menu
 183      */
 184     public void setHelpMenu(final Menu m) {
 185         synchronized (getTreeLock()) {
 186             if (helpMenu == m) {
 187                 return;
 188             }
 189             if (helpMenu != null) {
 190                 remove(helpMenu);
 191             }
 192             helpMenu = m;
 193             if (m != null) {
 194                 if (m.parent != this) {
 195                     add(m);
 196                 }


 197                 m.isHelpMenu = true;
 198                 m.parent = this;
 199                 MenuBarPeer peer = (MenuBarPeer)this.peer;
 200                 if (peer != null) {
 201                     if (m.peer == null) {
 202                         m.addNotify();
 203                     }
 204                     peer.addHelpMenu(m);
 205                 }
 206             }
 207         }
 208     }
 209 
 210     /**
 211      * Adds the specified menu to the menu bar.
 212      * If the menu has been part of another menu bar,
 213      * removes it from that menu bar.
 214      *
 215      * @param        m   the menu to be added
 216      * @return       the menu added


 225             menus.addElement(m);
 226             m.parent = this;
 227 
 228             MenuBarPeer peer = (MenuBarPeer)this.peer;
 229             if (peer != null) {
 230                 if (m.peer == null) {
 231                     m.addNotify();
 232                 }
 233                 peer.addMenu(m);
 234             }
 235             return m;
 236         }
 237     }
 238 
 239     /**
 240      * Removes the menu located at the specified
 241      * index from this menu bar.
 242      * @param        index   the position of the menu to be removed.
 243      * @see          java.awt.MenuBar#add(java.awt.Menu)
 244      */
 245     public void remove(final int index) {
 246         synchronized (getTreeLock()) {
 247             Menu m = getMenu(index);
 248             menus.removeElementAt(index);
 249             MenuBarPeer peer = (MenuBarPeer)this.peer;
 250             if (peer != null) {
 251                 m.removeNotify();
 252                 m.parent = null;
 253                 peer.delMenu(index);
 254             }
 255             if (helpMenu == m) {
 256                 helpMenu = null;
 257                 m.isHelpMenu = false;
 258             }
 259         }
 260     }
 261 
 262     /**
 263      * Removes the specified menu component from this menu bar.
 264      * @param        m the menu component to be removed.
 265      * @see          java.awt.MenuBar#add(java.awt.Menu)
 266      */
 267     public void remove(MenuComponent m) {
 268         synchronized (getTreeLock()) {
 269             int index = menus.indexOf(m);
 270             if (index >= 0) {
 271                 remove(index);
 272             }
 273         }
 274     }
 275 
 276     /**
 277      * Gets the number of menus on the menu bar.