CheckboxMenuItem (Java Platform SE 6) (original) (raw)
java.awt
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:
JDK1.0
See Also:
ItemEvent, ItemListener, Serialized Form
Nested Class Summary | |
---|---|
protected class | CheckboxMenuItem.AccessibleAWTCheckboxMenuItem Inner class of CheckboxMenuItem used to provide default support for accessibility. |
Nested classes/interfaces inherited from class java.awt.MenuItem |
---|
MenuItem.AccessibleAWTMenuItem |
Nested classes/interfaces inherited from class java.awt.MenuComponent |
---|
MenuComponent.AccessibleAWTMenuComponent |
Constructor Summary |
---|
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](../../java/awt/CheckboxMenuItem.html#CheckboxMenuItem%28java.lang.String, boolean%29)(String label, boolean state) Create a check box menu item with the specified label and state. |
Method Summary | |
---|---|
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 specifed state. |
Methods inherited from class java.awt.MenuItem |
---|
addActionListener, deleteShortcut, disable, disableEvents, enable, enable, enableEvents, getActionCommand, getActionListeners, getLabel, getShortcut, isEnabled, processActionEvent, removeActionListener, setActionCommand, setEnabled, setLabel, setShortcut |
Methods inherited from class java.awt.MenuComponent |
---|
dispatchEvent, getFont, getName, getParent, getPeer, getTreeLock, postEvent, removeNotify, setFont, setName, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, [wait](../../java/lang/Object.html#wait%28long, int%29) |
Constructor Detail |
---|
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](../../java/awt/HeadlessException.html "class in java.awt")
- if GraphicsEnvironment.isHeadless() returns true
Since:
JDK1.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](../../java/awt/HeadlessException.html "class in java.awt")
- 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, wheretrue
indicates "on" andfalse
indicates "off."
Throws:
[HeadlessException](../../java/awt/HeadlessException.html "class in java.awt")
- if GraphicsEnvironment.isHeadless() returns true
Since:
JDK1.1
See Also:
GraphicsEnvironment.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%28%29)
in class [MenuItem](../../java/awt/MenuItem.html "class in java.awt")
See Also:
Toolkit.createCheckboxMenuItem(java.awt.CheckboxMenuItem), 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, wheretrue
indicates "on" andfalse
indicates "off"
See Also:
setState
public void setState(boolean b)
Sets this check box menu item to the specifed state. The boolean value true
indicates "on" whilefalse
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 anItemEvent
is by user interaction.
Parameters:
b
- true
if the check box menu item is on, otherwise false
See Also:
getSelectedObjects
public Object[] 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%28%29)
in interface [ItemSelectable](../../java/awt/ItemSelectable.html "interface in java.awt")
See Also:
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](../../java/awt/ItemSelectable.html#addItemListener%28java.awt.event.ItemListener%29)
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), 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](../../java/awt/ItemSelectable.html#removeItemListener%28java.awt.event.ItemListener%29)
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), 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(java.awt.event.ItemListener), removeItemListener(java.awt.event.ItemListener), ItemEvent, ItemListener
getListeners
public <T extends EventListener> T[] getListeners(Class listenerType)
Returns an array of all the objects currently registered as _Foo_Listener
s upon this CheckboxMenuItem
._Foo_Listener
s are registered using theadd_Foo_Listener
method.
You can specify the listenerType
argument with a class literal, such as_Foo_Listener.class
. For example, you can query aCheckboxMenuItem
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%28java.lang.Class%29)
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 fromjava.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 implementsjava.util.EventListener
Since:
1.3
See Also:
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](../../java/awt/MenuItem.html#processEvent%28java.awt.AWTEvent%29)
in class [MenuItem](../../java/awt/MenuItem.html "class in java.awt")
Parameters:
e
- the event
Since:
JDK1.1
See Also:
ItemEvent, processItemEvent(java.awt.event.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 viaaddItemListener
. - 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, ItemListener, addItemListener(java.awt.event.ItemListener), MenuItem.enableEvents(long)
paramString
public String paramString()
Returns a string representing the state of thisCheckBoxMenuItem
. 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%28%29)
in class [MenuItem](../../java/awt/MenuItem.html "class in java.awt")
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](../../javax/accessibility/Accessible.html#getAccessibleContext%28%29)
in interface [Accessible](../../javax/accessibility/Accessible.html "interface in javax.accessibility")
Overrides:
[getAccessibleContext](../../java/awt/MenuItem.html#getAccessibleContext%28%29)
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 Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2015, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
Scripting on this page tracks web page traffic, but does not change the content in any way.