CheckboxMenuItem (Java Platform SE 8 ) (original) (raw)
- java.awt.MenuComponent
- java.awt.MenuItem
- java.awt.CheckboxMenuItem
- java.awt.MenuItem
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:
JDK1.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 inherited from class java.awt.[MenuItem](../../java/awt/MenuItem.html "class in java.awt") `[MenuItem.AccessibleAWTMenuItem](../../java/awt/MenuItem.AccessibleAWTMenuItem.html "class in java.awt")` * ### Nested classes/interfaces inherited from class java.awt.[MenuComponent](../../java/awt/MenuComponent.html "class in java.awt") `[MenuComponent.AccessibleAWTMenuComponent](../../java/awt/MenuComponent.AccessibleAWTMenuComponent.html "class in java.awt")`
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
All Methods Instance Methods Concrete Methods
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 listenerType) Returns an array of all the objects currently registered as _Foo_Listeners upon this CheckboxMenuItem. Object[] getSelectedObjects() Returns the an 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 thisCheckBoxMenuItem. 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 inherited from class java.awt.[MenuItem](../../java/awt/MenuItem.html "class in java.awt") `[addActionListener](../../java/awt/MenuItem.html#addActionListener-java.awt.event.ActionListener-), [deleteShortcut](../../java/awt/MenuItem.html#deleteShortcut--), [disable](../../java/awt/MenuItem.html#disable--), [disableEvents](../../java/awt/MenuItem.html#disableEvents-long-), [enable](../../java/awt/MenuItem.html#enable--), [enable](../../java/awt/MenuItem.html#enable-boolean-), [enableEvents](../../java/awt/MenuItem.html#enableEvents-long-), [getActionCommand](../../java/awt/MenuItem.html#getActionCommand--), [getActionListeners](../../java/awt/MenuItem.html#getActionListeners--), [getLabel](../../java/awt/MenuItem.html#getLabel--), [getShortcut](../../java/awt/MenuItem.html#getShortcut--), [isEnabled](../../java/awt/MenuItem.html#isEnabled--), [processActionEvent](../../java/awt/MenuItem.html#processActionEvent-java.awt.event.ActionEvent-), [removeActionListener](../../java/awt/MenuItem.html#removeActionListener-java.awt.event.ActionListener-), [setActionCommand](../../java/awt/MenuItem.html#setActionCommand-java.lang.String-), [setEnabled](../../java/awt/MenuItem.html#setEnabled-boolean-), [setLabel](../../java/awt/MenuItem.html#setLabel-java.lang.String-), [setShortcut](../../java/awt/MenuItem.html#setShortcut-java.awt.MenuShortcut-)` * ### Methods inherited from class java.awt.[MenuComponent](../../java/awt/MenuComponent.html "class in java.awt") `[dispatchEvent](../../java/awt/MenuComponent.html#dispatchEvent-java.awt.AWTEvent-), [getFont](../../java/awt/MenuComponent.html#getFont--), [getName](../../java/awt/MenuComponent.html#getName--), [getParent](../../java/awt/MenuComponent.html#getParent--), [getPeer](../../java/awt/MenuComponent.html#getPeer--), [getTreeLock](../../java/awt/MenuComponent.html#getTreeLock--), [postEvent](../../java/awt/MenuComponent.html#postEvent-java.awt.Event-), [removeNotify](../../java/awt/MenuComponent.html#removeNotify--), [setFont](../../java/awt/MenuComponent.html#setFont-java.awt.Font-), [setName](../../java/awt/MenuComponent.html#setName-java.lang.String-), [toString](../../java/awt/MenuComponent.html#toString--)` * ### Methods inherited from class java.lang.[Object](../../java/lang/Object.html "class in java.lang") `[clone](../../java/lang/Object.html#clone--), [equals](../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../java/lang/Object.html#finalize--), [getClass](../../java/lang/Object.html#getClass--), [hashCode](../../java/lang/Object.html#hashCode--), [notify](../../java/lang/Object.html#notify--), [notifyAll](../../java/lang/Object.html#notifyAll--), [wait](../../java/lang/Object.html#wait--), [wait](../../java/lang/Object.html#wait-long-), [wait](../../java/lang/Object.html#wait-long-int-)`
Constructor Detail
* #### CheckboxMenuItem public CheckboxMenuItem() throws [HeadlessException](../../java/awt/HeadlessException.html "class in java.awt") Create a check box menu item with an empty label. The item's state is initially set to "off." Throws: `[HeadlessException](../../java/awt/HeadlessException.html "class in java.awt")` \- if GraphicsEnvironment.isHeadless() returns true Since: JDK1.1 See Also: [GraphicsEnvironment.isHeadless()](../../java/awt/GraphicsEnvironment.html#isHeadless--) * #### CheckboxMenuItem public CheckboxMenuItem([String](../../java/lang/String.html "class in java.lang") label) throws [HeadlessException](../../java/awt/HeadlessException.html "class in java.awt") 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](../../java/awt/HeadlessException.html "class in java.awt")` \- if GraphicsEnvironment.isHeadless() returns true See Also: [GraphicsEnvironment.isHeadless()](../../java/awt/GraphicsEnvironment.html#isHeadless--) * #### CheckboxMenuItem public CheckboxMenuItem([String](../../java/lang/String.html "class in java.lang") label, boolean state) throws [HeadlessException](../../java/awt/HeadlessException.html "class in java.awt") 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](../../java/awt/HeadlessException.html "class in java.awt")` \- if GraphicsEnvironment.isHeadless() returns true Since: JDK1.1 See Also: [GraphicsEnvironment.isHeadless()](../../java/awt/GraphicsEnvironment.html#isHeadless--)
Method Detail
* #### 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](../../java/awt/MenuItem.html#addNotify--)` in class `[MenuItem](../../java/awt/MenuItem.html "class in java.awt")` See Also: [Toolkit.createCheckboxMenuItem(java.awt.CheckboxMenuItem)](../../java/awt/Toolkit.html#createCheckboxMenuItem-java.awt.CheckboxMenuItem-), [Component.getToolkit()](../../java/awt/Component.html#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)](../../java/awt/CheckboxMenuItem.html#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()](../../java/awt/CheckboxMenuItem.html#getState--) * #### getSelectedObjects public [Object](../../java/lang/Object.html "class in java.lang")[] getSelectedObjects() Returns the an array (length 1) containing the checkbox menu item label or null if the checkbox is not selected. Specified by: `[getSelectedObjects](../../java/awt/ItemSelectable.html#getSelectedObjects--)` in interface `[ItemSelectable](../../java/awt/ItemSelectable.html "interface in java.awt")` See Also: [ItemSelectable](../../java/awt/ItemSelectable.html "interface in java.awt") * #### addItemListener public void addItemListener([ItemListener](../../java/awt/event/ItemListener.html "interface in java.awt.event") 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](doc-files/AWTThreadIssues.html#ListenersThreads) for details on AWT's threading model. Specified by: `[addItemListener](../../java/awt/ItemSelectable.html#addItemListener-java.awt.event.ItemListener-)` in interface `[ItemSelectable](../../java/awt/ItemSelectable.html "interface in java.awt")` Parameters: `l` \- the item listener Since: JDK1.1 See Also: [removeItemListener(java.awt.event.ItemListener)](../../java/awt/CheckboxMenuItem.html#removeItemListener-java.awt.event.ItemListener-), [getItemListeners()](../../java/awt/CheckboxMenuItem.html#getItemListeners--), [setState(boolean)](../../java/awt/CheckboxMenuItem.html#setState-boolean-), [ItemEvent](../../java/awt/event/ItemEvent.html "class in java.awt.event"), [ItemListener](../../java/awt/event/ItemListener.html "interface in java.awt.event") * #### removeItemListener public void removeItemListener([ItemListener](../../java/awt/event/ItemListener.html "interface in java.awt.event") 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](doc-files/AWTThreadIssues.html#ListenersThreads) for details on AWT's threading model. Specified by: `[removeItemListener](../../java/awt/ItemSelectable.html#removeItemListener-java.awt.event.ItemListener-)` in interface `[ItemSelectable](../../java/awt/ItemSelectable.html "interface in java.awt")` Parameters: `l` \- the item listener Since: JDK1.1 See Also: [addItemListener(java.awt.event.ItemListener)](../../java/awt/CheckboxMenuItem.html#addItemListener-java.awt.event.ItemListener-), [getItemListeners()](../../java/awt/CheckboxMenuItem.html#getItemListeners--), [ItemEvent](../../java/awt/event/ItemEvent.html "class in java.awt.event"), [ItemListener](../../java/awt/event/ItemListener.html "interface in java.awt.event") * #### getItemListeners public [ItemListener](../../java/awt/event/ItemListener.html "interface in java.awt.event")[] 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(java.awt.event.ItemListener)](../../java/awt/CheckboxMenuItem.html#addItemListener-java.awt.event.ItemListener-), [removeItemListener(java.awt.event.ItemListener)](../../java/awt/CheckboxMenuItem.html#removeItemListener-java.awt.event.ItemListener-), [ItemEvent](../../java/awt/event/ItemEvent.html "class in java.awt.event"), [ItemListener](../../java/awt/event/ItemListener.html "interface in java.awt.event") * #### getListeners public <T extends [EventListener](../../java/util/EventListener.html "interface in java.util")> T[] getListeners([Class](../../java/lang/Class.html "class in java.lang")<T> listenerType) Returns an array of all the objects currently registered as `_Foo_Listener`s upon this `CheckboxMenuItem`.`_Foo_Listener`s are registered using the`add_Foo_Listener` method. You can specify the `listenerType` argument with a class literal, such as`_Foo_Listener.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](../../java/awt/MenuItem.html#getListeners-java.lang.Class-)` in class `[MenuItem](../../java/awt/MenuItem.html "class in java.awt")` 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`_Foo_Listener`s on this checkbox menuitem, or an empty array if no such listeners have been added Throws: `[ClassCastException](../../java/lang/ClassCastException.html "class in java.lang")` \- if `listenerType` doesn't specify a class or interface that implements`java.util.EventListener` Since: 1.3 See Also: [getItemListeners()](../../java/awt/CheckboxMenuItem.html#getItemListeners--) * #### processEvent protected void processEvent([AWTEvent](../../java/awt/AWTEvent.html "class in java.awt") 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](../../java/awt/MenuItem.html#processEvent-java.awt.AWTEvent-)` in class `[MenuItem](../../java/awt/MenuItem.html "class in java.awt")` Parameters: `e` \- the event Since: JDK1.1 See Also: [ItemEvent](../../java/awt/event/ItemEvent.html "class in java.awt.event"), [processItemEvent(java.awt.event.ItemEvent)](../../java/awt/CheckboxMenuItem.html#processItemEvent-java.awt.event.ItemEvent-) * #### processItemEvent protected void processItemEvent([ItemEvent](../../java/awt/event/ItemEvent.html "class in java.awt.event") 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: JDK1.1 See Also: [ItemEvent](../../java/awt/event/ItemEvent.html "class in java.awt.event"), [ItemListener](../../java/awt/event/ItemListener.html "interface in java.awt.event"), [addItemListener(java.awt.event.ItemListener)](../../java/awt/CheckboxMenuItem.html#addItemListener-java.awt.event.ItemListener-), [MenuItem.enableEvents(long)](../../java/awt/MenuItem.html#enableEvents-long-) * #### paramString public [String](../../java/lang/String.html "class in java.lang") 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](../../java/awt/MenuItem.html#paramString--)` in class `[MenuItem](../../java/awt/MenuItem.html "class in java.awt")` Returns: the parameter string of this check box menu item * #### getAccessibleContext public [AccessibleContext](../../javax/accessibility/AccessibleContext.html "class in javax.accessibility") 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](../../javax/accessibility/Accessible.html#getAccessibleContext--)` in interface `[Accessible](../../javax/accessibility/Accessible.html "interface in javax.accessibility")` Overrides: `[getAccessibleContext](../../java/awt/MenuItem.html#getAccessibleContext--)` in class `[MenuItem](../../java/awt/MenuItem.html "class in java.awt")` Returns: an AccessibleAWTCheckboxMenuItem that serves as the AccessibleContext of this CheckboxMenuItem Since: 1.3
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.