public class CheckboxMenuItem extends MenuItemimplements ItemSelectable , Accessible
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.
ItemEvent,
ItemListener,
Serialized Form
| Modifier and Type | Class and Description |
|---|---|
protected class |
CheckboxMenuItem
Inner class of CheckboxMenuItem used to provide default support for accessibility.
|
MenuItem.AccessibleAWTMenuItem MenuComponent.AccessibleAWTMenuComponent | Constructor and Description |
|---|
CheckboxMenuItem()
Create a check box menu item with an empty label.
|
CheckboxMenuItem(String
Create a check box menu item with the specified label.
|
CheckboxMenuItem(String
Create a check box menu item with the specified label and state.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addItemListener(ItemListener
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 |
getListeners(Class
Returns an array of all the objects currently registered as
FooListeners 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 this
CheckBoxMenuItem.
|
protected void |
processEvent(AWTEvent
Processes events on this check box menu item.
|
protected void |
processItemEvent(ItemEvent
Processes item events occurring on this check box menu item by dispatching them to any registered
ItemListener objects.
|
void |
removeItemListener(ItemListener
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.
|
addActionListener, deleteShortcut, disable, disableEvents, enable, enable, enableEvents, getActionCommand, getActionListeners, getLabel, getShortcut, isEnabled, processActionEvent, removeActionListener, setActionCommand, setEnabled, setLabel, setShortcutdispatchEvent, getFont, getName, getParent, getPeer, getTreeLock, postEvent, removeNotify, setFont, setName, toStringpublic CheckboxMenuItem()
throws HeadlessException
HeadlessException - if GraphicsEnvironment.isHeadless() returns true
GraphicsEnvironment.isHeadless()
public CheckboxMenuItem(Stringlabel) throws HeadlessException
label - a string label for the check box menu item, or
null for an unlabeled menu item.
HeadlessException - if GraphicsEnvironment.isHeadless() returns true
GraphicsEnvironment.isHeadless()
public CheckboxMenuItem(Stringlabel, boolean state) throws HeadlessException
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."
HeadlessException - if GraphicsEnvironment.isHeadless() returns true
GraphicsEnvironment.isHeadless()
public void addNotify()
addNotify in class
MenuItem
Toolkit.createCheckboxMenuItem(java.awt.CheckboxMenuItem) ,
Component.getToolkit()
public boolean getState()
true indicates "on" and
false indicates "off"
setState(boolean)
public void setState(boolean b)
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.
b -
true if the check box menu item is on, otherwise
false
getState()
public Object[] getSelectedObjects()
getSelectedObjects in interface
ItemSelectable
ItemSelectable
public void addItemListener(ItemListenerl)
Refer to AWT Threading Issues for details on AWT's threading model.
addItemListener in interface
ItemSelectable
l - the item listener
removeItemListener(java.awt.event.ItemListener),
getItemListeners(),
setState(boolean),
ItemEvent,
ItemListener
public void removeItemListener(ItemListenerl)
Refer to AWT Threading Issues for details on AWT's threading model.
removeItemListener in interface
ItemSelectable
l - the item listener
addItemListener(java.awt.event.ItemListener),
getItemListeners(),
ItemEvent,
ItemListener
public ItemListener[] getItemListeners()
ItemListeners or an empty array if no item listeners are currently registered
addItemListener(java.awt.event.ItemListener),
removeItemListener(java.awt.event.ItemListener),
ItemEvent,
ItemListener
public <T extends EventListener> T[] getListeners(Class <T> listenerType)
FooListeners upon this
CheckboxMenuItem.
FooListeners 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.
getListeners in class
MenuItem
listenerType - the type of listeners requested; this parameter should specify an interface that descends from
java.util.EventListener
FooListeners on this checkbox menuitem, or an empty array if no such listeners have been added
ClassCastException - if
listenerType doesn't specify a class or interface that implements
java.util.EventListener
getItemListeners()
protected void processEvent(AWTEvente)
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.
processEvent in class
MenuItem
e - the event
ItemEvent,
processItemEvent(java.awt.event.ItemEvent)
protected void processItemEvent(ItemEvente)
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:
ItemListener object is registered via addItemListener. enableEvents. Note that if the event parameter is null the behavior is unspecified and may result in an exception.
e - the item event
ItemEvent,
ItemListener,
addItemListener(java.awt.event.ItemListener),
MenuItem.enableEvents(long)
public StringparamString()
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.
paramString in class
MenuItem
public AccessibleContextgetAccessibleContext()
getAccessibleContext in interface
Accessible
getAccessibleContext in class
MenuItem