< prev index next >

src/java.desktop/share/classes/java/awt/CheckboxMenuItem.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.CheckboxMenuItemPeer;
  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 /**
  38  * This class represents a check box that can be included in a menu.
  39  * Selecting the check box in the menu changes its state from
  40  * "on" to "off" or from "off" to "on."
  41  * <p>
  42  * The following picture depicts a menu which contains an instance
  43  * of {@code CheckBoxMenuItem}:
  44  * <p>
  45  * <img src="doc-files/MenuBar-1.gif"
  46  * alt="Menu labeled Examples, containing items Basic, Simple, Check, and More Examples. The Check item is a CheckBoxMenuItem instance, in the off state."

  47  * style="float:center; margin: 7px 10px;">
  48  * <p>
  49  * The item labeled {@code Check} shows a check box menu item
  50  * in its "off" state.
  51  * <p>
  52  * When a check box menu item is selected, AWT sends an item event to
  53  * the item. Since the event is an instance of {@code ItemEvent},
  54  * the {@code processEvent} method examines the event and passes
  55  * it along to {@code processItemEvent}. The latter method redirects
  56  * the event to any {@code ItemListener} objects that have
  57  * registered an interest in item events generated by this menu item.
  58  *
  59  * @author      Sami Shaio
  60  * @see         java.awt.event.ItemEvent
  61  * @see         java.awt.event.ItemListener
  62  * @since       1.0
  63  */
  64 public class CheckboxMenuItem extends MenuItem implements ItemSelectable, Accessible {
  65 
  66     static {
  67         /* ensure that the necessary native libraries are loaded */
  68         Toolkit.loadLibraries();
  69         if (!GraphicsEnvironment.isHeadless()) {
  70             initIDs();
  71         }
  72 
  73         AWTAccessor.setCheckboxMenuItemAccessor(
  74             new AWTAccessor.CheckboxMenuItemAccessor() {
  75                 public boolean getState(CheckboxMenuItem cmi) {
  76                     return cmi.state;
  77                 }
  78             });
  79     }
  80 
  81    /**
  82     * The state of a checkbox menu item
  83     * @serial
  84     * @see #getState()
  85     * @see #setState(boolean)
  86     */
  87     boolean state = false;
  88 
  89     transient ItemListener itemListener;
  90 
  91     private static final String base = "chkmenuitem";
  92     private static int nameCounter = 0;
  93 
  94     /*
  95      * JDK 1.1 serialVersionUID
  96      */
  97      private static final long serialVersionUID = 6190621106981774043L;
  98 
  99     /**
 100      * Create a check box menu item with an empty label.
 101      * The item's state is initially set to "off."
 102      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 103      * returns true
 104      * @see java.awt.GraphicsEnvironment#isHeadless
 105      * @since   1.1
 106      */
 107     public CheckboxMenuItem() throws HeadlessException {
 108         this("", false);
 109     }


   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.ItemEvent;
  29 import java.awt.event.ItemListener;
  30 import java.awt.peer.CheckboxMenuItemPeer;




  31 import java.io.IOException;
  32 import java.io.ObjectInputStream;
  33 import java.io.ObjectOutputStream;
  34 import java.util.EventListener;
  35 
  36 import javax.accessibility.Accessible;
  37 import javax.accessibility.AccessibleAction;
  38 import javax.accessibility.AccessibleContext;
  39 import javax.accessibility.AccessibleRole;
  40 import javax.accessibility.AccessibleValue;
  41 
  42 import sun.awt.AWTAccessor;
  43 
  44 /**
  45  * This class represents a check box that can be included in a menu.
  46  * Selecting the check box in the menu changes its state from
  47  * "on" to "off" or from "off" to "on."
  48  * <p>
  49  * The following picture depicts a menu which contains an instance
  50  * of {@code CheckBoxMenuItem}:
  51  * <p>
  52  * <img src="doc-files/MenuBar-1.gif"
  53  * alt="Menu labeled Examples, containing items Basic, Simple, Check, and More
  54  * Examples. The Check item is a CheckBoxMenuItem instance, in the off state."
  55  * style="float:center; margin: 7px 10px;">
  56  * <p>
  57  * The item labeled {@code Check} shows a check box menu item
  58  * in its "off" state.
  59  * <p>
  60  * When a check box menu item is selected, AWT sends an item event to
  61  * the item. Since the event is an instance of {@code ItemEvent},
  62  * the {@code processEvent} method examines the event and passes
  63  * it along to {@code processItemEvent}. The latter method redirects
  64  * the event to any {@code ItemListener} objects that have
  65  * registered an interest in item events generated by this menu item.
  66  *
  67  * @author      Sami Shaio
  68  * @see         java.awt.event.ItemEvent
  69  * @see         java.awt.event.ItemListener
  70  * @since       1.0
  71  */
  72 public class CheckboxMenuItem extends MenuItem implements ItemSelectable, Accessible {
  73 
  74     static {
  75         /* ensure that the necessary native libraries are loaded */
  76         Toolkit.loadLibraries();
  77         if (!GraphicsEnvironment.isHeadless()) {
  78             initIDs();
  79         }
  80 
  81         AWTAccessor.setCheckboxMenuItemAccessor(
  82             new AWTAccessor.CheckboxMenuItemAccessor() {
  83                 public boolean getState(CheckboxMenuItem cmi) {
  84                     return cmi.state;
  85                 }
  86             });
  87     }
  88 
  89    /**
  90     * The state of a checkbox menu item
  91     * @serial
  92     * @see #getState()
  93     * @see #setState(boolean)
  94     */
  95     private volatile boolean state;
  96 
  97     private transient volatile ItemListener itemListener;
  98 
  99     private static final String base = "chkmenuitem";
 100     private static int nameCounter = 0;
 101 
 102     /*
 103      * JDK 1.1 serialVersionUID
 104      */
 105      private static final long serialVersionUID = 6190621106981774043L;
 106 
 107     /**
 108      * Create a check box menu item with an empty label.
 109      * The item's state is initially set to "off."
 110      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 111      * returns true
 112      * @see java.awt.GraphicsEnvironment#isHeadless
 113      * @since   1.1
 114      */
 115     public CheckboxMenuItem() throws HeadlessException {
 116         this("", false);
 117     }


< prev index next >