< prev index next >

src/java.desktop/share/classes/java/awt/MenuItem.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.awt.peer.MenuItemPeer;
  28 import java.awt.event.*;
  29 import java.util.EventListener;
  30 import java.io.ObjectOutputStream;
  31 import java.io.ObjectInputStream;
  32 import java.io.IOException;
  33 import javax.accessibility.*;









  34 import sun.awt.AWTAccessor;
  35 
  36 /**
  37  * All items in a menu must belong to the class
  38  * {@code MenuItem}, or one of its subclasses.
  39  * <p>
  40  * The default {@code MenuItem} object embodies
  41  * a simple labeled menu item.
  42  * <p>
  43  * This picture of a menu bar shows five menu items:
  44  * <IMG SRC="doc-files/MenuBar-1.gif" alt="The following text describes this graphic."
  45  * style="float:center; margin: 7px 10px;">
  46  * <br style="clear:left;">
  47  * The first two items are simple menu items, labeled
  48  * {@code "Basic"} and {@code "Simple"}.
  49  * Following these two items is a separator, which is itself
  50  * a menu item, created with the label {@code "-"}.
  51  * Next is an instance of {@code CheckboxMenuItem}
  52  * labeled {@code "Check"}. The final menu item is a
  53  * submenu labeled <code>"More&nbsp;Examples"</code>,


  94                 public String getActionCommandImpl(MenuItem item) {
  95                     return item.getActionCommandImpl();
  96                 }
  97 
  98                 public boolean isItemEnabled(MenuItem item) {
  99                     return item.isItemEnabled();
 100                 }
 101             });
 102     }
 103 
 104     /**
 105      * A value to indicate whether a menu item is enabled
 106      * or not.  If it is enabled, {@code enabled} will
 107      * be set to true.  Else {@code enabled} will
 108      * be set to false.
 109      *
 110      * @serial
 111      * @see #isEnabled()
 112      * @see #setEnabled(boolean)
 113      */
 114     boolean enabled = true;
 115 
 116     /**
 117      * {@code label} is the label of a menu item.
 118      * It can be any string.
 119      *
 120      * @serial
 121      * @see #getLabel()
 122      * @see #setLabel(String)
 123      */
 124     String label;
 125 
 126     /**
 127      * This field indicates the command that has been issued
 128      * by a  particular menu item.
 129      * By default the {@code actionCommand}
 130      * is the label of the menu item, unless it has been
 131      * set using setActionCommand.
 132      *
 133      * @serial
 134      * @see #setActionCommand(String)
 135      * @see #getActionCommand()
 136      */
 137     String actionCommand;
 138 
 139     /**
 140      * The eventMask is ONLY set by subclasses via enableEvents.
 141      * The mask should NOT be set when listeners are registered
 142      * so that we can distinguish the difference between when
 143      * listeners request events and subclasses request them.
 144      *
 145      * @serial
 146      */
 147     long eventMask;
 148 
 149     transient ActionListener actionListener;
 150 
 151     /**
 152      * A sequence of key stokes that ia associated with
 153      * a menu item.
 154      * Note :in 1.1.2 you must use setActionCommand()
 155      * on a menu item in order for its shortcut to
 156      * work.
 157      *
 158      * @serial
 159      * @see #getShortcut()
 160      * @see #setShortcut(MenuShortcut)
 161      * @see #deleteShortcut()
 162      */
 163     private MenuShortcut shortcut = null;
 164 
 165     private static final String base = "menuitem";
 166     private static int nameCounter = 0;
 167 
 168     /*
 169      * JDK 1.1 serialVersionUID
 170      */
 171     private static final long serialVersionUID = -21757335363267194L;
 172 
 173     /**
 174      * Constructs a new MenuItem with an empty label and no keyboard
 175      * shortcut.
 176      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 177      * returns true.
 178      * @see java.awt.GraphicsEnvironment#isHeadless
 179      * @since    1.1
 180      */
 181     public MenuItem() throws HeadlessException {
 182         this("", null);
 183     }


   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.ActionEvent;
  29 import java.awt.event.ActionListener;
  30 import java.awt.event.InputEvent;
  31 import java.awt.event.KeyEvent;
  32 import java.awt.peer.MenuItemPeer;




  33 import java.io.IOException;
  34 import java.io.ObjectInputStream;
  35 import java.io.ObjectOutputStream;
  36 import java.util.EventListener;
  37 
  38 import javax.accessibility.Accessible;
  39 import javax.accessibility.AccessibleAction;
  40 import javax.accessibility.AccessibleContext;
  41 import javax.accessibility.AccessibleRole;
  42 import javax.accessibility.AccessibleValue;
  43 
  44 import sun.awt.AWTAccessor;
  45 
  46 /**
  47  * All items in a menu must belong to the class
  48  * {@code MenuItem}, or one of its subclasses.
  49  * <p>
  50  * The default {@code MenuItem} object embodies
  51  * a simple labeled menu item.
  52  * <p>
  53  * This picture of a menu bar shows five menu items:
  54  * <IMG SRC="doc-files/MenuBar-1.gif" alt="The following text describes this graphic."
  55  * style="float:center; margin: 7px 10px;">
  56  * <br style="clear:left;">
  57  * The first two items are simple menu items, labeled
  58  * {@code "Basic"} and {@code "Simple"}.
  59  * Following these two items is a separator, which is itself
  60  * a menu item, created with the label {@code "-"}.
  61  * Next is an instance of {@code CheckboxMenuItem}
  62  * labeled {@code "Check"}. The final menu item is a
  63  * submenu labeled <code>"More&nbsp;Examples"</code>,


 104                 public String getActionCommandImpl(MenuItem item) {
 105                     return item.getActionCommandImpl();
 106                 }
 107 
 108                 public boolean isItemEnabled(MenuItem item) {
 109                     return item.isItemEnabled();
 110                 }
 111             });
 112     }
 113 
 114     /**
 115      * A value to indicate whether a menu item is enabled
 116      * or not.  If it is enabled, {@code enabled} will
 117      * be set to true.  Else {@code enabled} will
 118      * be set to false.
 119      *
 120      * @serial
 121      * @see #isEnabled()
 122      * @see #setEnabled(boolean)
 123      */
 124     private volatile boolean enabled = true;
 125 
 126     /**
 127      * {@code label} is the label of a menu item.
 128      * It can be any string.
 129      *
 130      * @serial
 131      * @see #getLabel()
 132      * @see #setLabel(String)
 133      */
 134     volatile String label;
 135 
 136     /**
 137      * This field indicates the command that has been issued
 138      * by a  particular menu item.
 139      * By default the {@code actionCommand}
 140      * is the label of the menu item, unless it has been
 141      * set using setActionCommand.
 142      *
 143      * @serial
 144      * @see #setActionCommand(String)
 145      * @see #getActionCommand()
 146      */
 147     private volatile String actionCommand;
 148 
 149     /**
 150      * The eventMask is ONLY set by subclasses via enableEvents.
 151      * The mask should NOT be set when listeners are registered
 152      * so that we can distinguish the difference between when
 153      * listeners request events and subclasses request them.
 154      *
 155      * @serial
 156      */
 157     volatile long eventMask;
 158 
 159     private transient volatile ActionListener actionListener;
 160 
 161     /**
 162      * A sequence of key stokes that ia associated with
 163      * a menu item.
 164      * Note :in 1.1.2 you must use setActionCommand()
 165      * on a menu item in order for its shortcut to
 166      * work.
 167      *
 168      * @serial
 169      * @see #getShortcut()
 170      * @see #setShortcut(MenuShortcut)
 171      * @see #deleteShortcut()
 172      */
 173     private volatile MenuShortcut shortcut;
 174 
 175     private static final String base = "menuitem";
 176     private static int nameCounter = 0;
 177 
 178     /*
 179      * JDK 1.1 serialVersionUID
 180      */
 181     private static final long serialVersionUID = -21757335363267194L;
 182 
 183     /**
 184      * Constructs a new MenuItem with an empty label and no keyboard
 185      * shortcut.
 186      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 187      * returns true.
 188      * @see java.awt.GraphicsEnvironment#isHeadless
 189      * @since    1.1
 190      */
 191     public MenuItem() throws HeadlessException {
 192         this("", null);
 193     }


< prev index next >