# Class CheckboxMenuItem

java.lang.Object
java.awt.MenuComponent
java.awt.MenuItem
java.awt.CheckboxMenuItem

All Implemented Interfaces:
  `ItemSelectable, Serializable, Accessible`

public class CheckboxMenuItem extends MenuItem implements ItemSelectable, Accessible
This class represents a check box that can be included in a menu. Selecting the check box in the menu changes its state from "on" to "off" or from "off" to "on."

The following picture depicts a menu which contains an instance of `CheckBoxMenuItem`:

The item labeled `Check` shows a check box menu item in its "off" state.

When a check box menu item is selected, AWT sends an item event to the item. Since the event is an instance of `ItemEvent`, the `processEvent` method examines the event and passes it along to `processItemEvent`. The latter method redirects the event to any `ItemListener` objects that have registered an interest in item events generated by this menu item.

Since:
  1.0
See Also:
  - `ItemEvent`
  - `ItemListener`
  - Serialized Form

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `protected class`
  `CheckboxMenuItem.AccessibleAWTCheckboxMenuItem`
  Inner class of CheckboxMenuItem used to provide default support for accessibility.

  ### Nested classes/interfaces declared in class MenuItem

  `MenuItem.AccessibleAWTMenuItem`
  Modifier and Type
  Class
  Description
  `protected class`
  `MenuItem.AccessibleAWTMenuItem`
  Inner class of MenuItem used to provide default support for accessibility.

  ### Nested classes/interfaces declared in class MenuComponent

  `MenuComponent.AccessibleAWTMenuComponent`
  Modifier and Type
  Class
  Description
  `protected class`
  `MenuComponent.AccessibleAWTMenuComponent`
  Inner class of `MenuComponent` used to provide default support for accessibility.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CheckboxMenuItem()`
  Create a check box menu item with an empty label.
  `CheckboxMenuItem(String label)`
  Create a check box menu item with the specified label.
  `CheckboxMenuItem(String label, boolean state)`
  Create a check box menu item with the specified label and state.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addItemListener(ItemListener l)`
  Adds the specified item listener to receive item events from this check box menu item.
  `void`
  `addNotify()`
  Creates the peer of the checkbox item.
  `AccessibleContext`
  `getAccessibleContext()`
  Gets the AccessibleContext associated with this CheckboxMenuItem.
  `ItemListener[]`
  `getItemListeners()`
  Returns an array of all the item listeners registered on this checkbox menuitem.
  `<T extends EventListener>
  T[]`
  `getListeners(Class<T> listenerType)`
  Returns an array of all the objects currently registered as `FooListener`s upon this `CheckboxMenuItem`.
  `Object[]`
  `getSelectedObjects()`
  Returns the array (length 1) containing the checkbox menu item label or null if the checkbox is not selected.
  `boolean`
  `getState()`
  Determines whether the state of this check box menu item is "on" or "off."
  `String`
  `paramString()`
  Returns a string representing the state of this `CheckBoxMenuItem`.
  `protected void`
  `processEvent(AWTEvent e)`
  Processes events on this check box menu item.
  `protected void`
  `processItemEvent(ItemEvent e)`
  Processes item events occurring on this check box menu item by dispatching them to any registered `ItemListener` objects.
  `void`
  `removeItemListener(ItemListener l)`
  Removes the specified item listener so that it no longer receives item events from this check box menu item.
  `void`
  `setState(boolean b)`
  Sets this check box menu item to the specified state.

  ### Methods declared in class MenuItem

  `addActionListener, deleteShortcut, disable, disableEvents, enable, enable, enableEvents, getActionCommand, getActionListeners, getLabel, getShortcut, isEnabled, processActionEvent, removeActionListener, setActionCommand, setEnabled, setLabel, setShortcut`
  Modifier and Type
  Method
  Description
  `void`
  `addActionListener(ActionListener l)`
  Adds the specified action listener to receive action events from this menu item.
  `void`
  `deleteShortcut()`
  Delete any `MenuShortcut` object associated with this menu item.
  `void`
  `disable()`
  Deprecated.
  As of JDK version 1.1, replaced by `setEnabled(boolean)`.
  `protected final void`
  `disableEvents(long eventsToDisable)`
  Disables event delivery to this menu item for events defined by the specified event mask parameter.
  `void`
  `enable()`
  Deprecated.
  As of JDK version 1.1, replaced by `setEnabled(boolean)`.
  `void`
  `enable(boolean b)`
  Deprecated.
  As of JDK version 1.1, replaced by `setEnabled(boolean)`.
  `protected final void`
  `enableEvents(long eventsToEnable)`
  Enables event delivery to this menu item for events to be defined by the specified event mask parameter
  `String`
  `getActionCommand()`
  Gets the command name of the action event that is fired by this menu item.
  `ActionListener[]`
  `getActionListeners()`
  Returns an array of all the action listeners registered on this menu item.
  `String`
  `getLabel()`
  Gets the label for this menu item.
  `MenuShortcut`
  `getShortcut()`
  Get the `MenuShortcut` object associated with this menu item,
  `boolean`
  `isEnabled()`
  Checks whether this menu item is enabled.
  `protected void`
  `processActionEvent(ActionEvent e)`
  Processes action events occurring on this menu item, by dispatching them to any registered `ActionListener` objects.
  `void`
  `removeActionListener(ActionListener l)`
  Removes the specified action listener so it no longer receives action events from this menu item.
  `void`
  `setActionCommand(String command)`
  Sets the command name of the action event that is fired by this menu item.
  `void`
  `setEnabled(boolean b)`
  Sets whether or not this menu item can be chosen.
  `void`
  `setLabel(String label)`
  Sets the label for this menu item to the specified label.
  `void`
  `setShortcut(MenuShortcut s)`
  Set the `MenuShortcut` object associated with this menu item.

  ### Methods declared in class MenuComponent

  `dispatchEvent, getFont, getName, getParent, getTreeLock, postEvent, removeNotify, setFont, setName, toString`
  Modifier and Type
  Method
  Description
  `final void`
  `dispatchEvent(AWTEvent e)`
  Delivers an event to this component or one of its sub components.
  `Font`
  `getFont()`
  Gets the font used for this menu component.
  `String`
  `getName()`
  Gets the name of the menu component.
  `MenuContainer`
  `getParent()`
  Returns the parent container for this menu component.
  `protected final Object`
  `getTreeLock()`
  Gets this component's locking object (the object that owns the thread synchronization monitor) for AWT component-tree and layout operations.
  `boolean`
  `postEvent(Event evt)`
  Deprecated.
  As of JDK version 1.1, replaced by `dispatchEvent`.
  `void`
  `removeNotify()`
  Removes the menu component's peer.
  `void`
  `setFont(Font f)`
  Sets the font to be used for this menu component to the specified font.
  `void`
  `setName(String name)`
  Sets the name of the component to the specified string.
  `String`
  `toString()`
  Returns a representation of this menu component as a string.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Constructor Details

  - ### CheckboxMenuItem

    public CheckboxMenuItem() throws HeadlessException
    Create a check box menu item with an empty label. The item's state is initially set to "off."
    Throws:
      `HeadlessException` - if GraphicsEnvironment.isHeadless() returns true
    Since:
      1.1
    See Also:
      - `GraphicsEnvironment.isHeadless()`

  - ### CheckboxMenuItem

    public CheckboxMenuItem(String label) throws HeadlessException
    Create a check box menu item with the specified label. The item's state is initially set to "off."
    Parameters:
      `label` - a string label for the check box menu item, or `null` for an unlabeled menu item.
    Throws:
      `HeadlessException` - if GraphicsEnvironment.isHeadless() returns true
    See Also:
      - `GraphicsEnvironment.isHeadless()`

  - ### CheckboxMenuItem

    public CheckboxMenuItem(String label, boolean state) throws HeadlessException
    Create a check box menu item with the specified label and state.
    Parameters:
      `label` - a string label for the check box menu item, or `null` for an unlabeled menu item.
      `state` - the initial state of the menu item, where `true` indicates "on" and `false` indicates "off."
    Throws:
      `HeadlessException` - if GraphicsEnvironment.isHeadless() returns true
    Since:
      1.1
    See Also:
      - `GraphicsEnvironment.isHeadless()`

- ## Method Details

  - ### addNotify

    public void addNotify()
    Creates the peer of the checkbox item. This peer allows us to change the look of the checkbox item without changing its functionality. Most applications do not call this method directly.
    Overrides:
      `addNotify` in class `MenuItem`
    See Also:
      - `Component.getToolkit()`

  - ### getState

    public boolean getState()
    Determines whether the state of this check box menu item is "on" or "off."
    Returns:
      the state of this check box menu item, where `true` indicates "on" and `false` indicates "off"
    See Also:
      - `setState(boolean)`

  - ### setState

    public void setState(boolean b)
    Sets this check box menu item to the specified state. The boolean value `true` indicates "on" while `false` indicates "off."

    Note that this method should be primarily used to initialize the state of the check box menu item. Programmatically setting the state of the check box menu item will not trigger an `ItemEvent`. The only way to trigger an `ItemEvent` is by user interaction.

    Parameters:
      `b` - `true` if the check box menu item is on, otherwise `false`
    See Also:
      - `getState()`

  - ### getSelectedObjects

    public Object[] getSelectedObjects()
    Returns the array (length 1) containing the checkbox menu item label or null if the checkbox is not selected.
    Specified by:
      `getSelectedObjects` in interface `ItemSelectable`
    Returns:
      the list of selected objects, or `null`
    See Also:
      - `ItemSelectable`

  - ### addItemListener

    public void addItemListener(ItemListener l)
    Adds the specified item listener to receive item events from this check box menu item. Item events are sent in response to user actions, but not in response to calls to setState(). If l is null, no exception is thrown and no action is performed.

    Refer to AWT Threading Issues for details on AWT's threading model.

    Specified by:
      `addItemListener` in interface `ItemSelectable`
    Parameters:
      `l` - the item listener
    Since:
      1.1
    See Also:
      - `removeItemListener(ItemListener)`
      - `getItemListeners()`
      - `setState(boolean)`
      - `ItemEvent`
      - `ItemListener`

  - ### removeItemListener

    public void removeItemListener(ItemListener l)
    Removes the specified item listener so that it no longer receives item events from this check box menu item. If l is null, no exception is thrown and no action is performed.

    Refer to AWT Threading Issues for details on AWT's threading model.

    Specified by:
      `removeItemListener` in interface `ItemSelectable`
    Parameters:
      `l` - the item listener
    Since:
      1.1
    See Also:
      - `addItemListener(ItemListener)`
      - `getItemListeners()`
      - `ItemEvent`
      - `ItemListener`

  - ### getItemListeners

    public ItemListener[] getItemListeners()
    Returns an array of all the item listeners registered on this checkbox menuitem.
    Returns:
      all of this checkbox menuitem's `ItemListener`s or an empty array if no item listeners are currently registered
    Since:
      1.4
    See Also:
      - `addItemListener(ItemListener)`
      - `removeItemListener(ItemListener)`
      - `ItemEvent`
      - `ItemListener`

  - ### getListeners

    public <T extends EventListener> T[] getListeners(Class<T> listenerType)
    Returns an array of all the objects currently registered as `FooListener`s upon this `CheckboxMenuItem`. `FooListener`s are registered using the `addFooListener` method.

    You can specify the `listenerType` argument with a class literal, such as `FooListener.class`. For example, you can query a `CheckboxMenuItem c` for its item listeners with the following code:

        ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class));

    If no such listeners exist, this method returns an empty array.
    Overrides:
      `getListeners` in class `MenuItem`
    Type Parameters:
      `T` - the type of the listeners
    Parameters:
      `listenerType` - the type of listeners requested; this parameter should specify an interface that descends from `java.util.EventListener`
    Returns:
      an array of all objects registered as `FooListener`s on this checkbox menuitem, or an empty array if no such listeners have been added
    Throws:
      `ClassCastException` - if `listenerType` doesn't specify a class or interface that implements `java.util.EventListener`
    Since:
      1.3
    See Also:
      - `getItemListeners()`

  - ### processEvent

    protected void processEvent(AWTEvent e)
    Processes events on this check box menu item. If the event is an instance of `ItemEvent`, this method invokes the `processItemEvent` method. If the event is not an item event, it invokes `processEvent` on the superclass.

    Check box menu items currently support only item events.

    Note that if the event parameter is `null` the behavior is unspecified and may result in an exception.

    Overrides:
      `processEvent` in class `MenuItem`
    Parameters:
      `e` - the event
    Since:
      1.1
    See Also:
      - `ItemEvent`
      - `processItemEvent(ItemEvent)`

  - ### processItemEvent

    protected void processItemEvent(ItemEvent e)
    Processes item events occurring on this check box menu item by dispatching them to any registered `ItemListener` objects.

    This method is not called unless item events are enabled for this menu item. Item events are enabled when one of the following occurs:

    - An `ItemListener` object is registered via `addItemListener`.
    - Item events are enabled via `enableEvents`.

    Note that if the event parameter is `null` the behavior is unspecified and may result in an exception.

    Parameters:
      `e` - the item event
    Since:
      1.1
    See Also:
      - `ItemEvent`
      - `ItemListener`
      - `addItemListener(ItemListener)`
      - `MenuItem.enableEvents(long)`

  - ### paramString

    public String paramString()
    Returns a string representing the state of this `CheckBoxMenuItem`. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be `null`.
    Overrides:
      `paramString` in class `MenuItem`
    Returns:
      the parameter string of this check box menu item

  - ### getAccessibleContext

    public AccessibleContext getAccessibleContext()
    Gets the AccessibleContext associated with this CheckboxMenuItem. For checkbox menu items, the AccessibleContext takes the form of an AccessibleAWTCheckboxMenuItem. A new AccessibleAWTCheckboxMenuItem is created if necessary.
    Specified by:
      `getAccessibleContext` in interface `Accessible`
    Overrides:
      `getAccessibleContext` in class `MenuItem`
    Returns:
      an AccessibleAWTCheckboxMenuItem that serves as the AccessibleContext of this CheckboxMenuItem
    Since:
      1.3