CheckboxMenuItem
The CheckboxMenuItem
is a subclass of MenuItem
that can be toggled. It is similar to a Checkbox
but appears on a Menu
. The appearance depends upon the platform. There may or may not be a visual indicator next to the choice. However, when the MenuItem
is selected (true
), a checkmark or some similar graphic will be displayed next to the label.
There is no way to put CheckboxMenuItem
components into a CheckboxGroup
to form a radio menu group.
An example of a CheckboxMenuItem
is the Show Java Console menu item in Netscape Navigator.
CheckboxMenuItem Methods
Constructors- public CheckboxMenuItem (String label)
- The first
CheckboxMenuItem
constructor creates aCheckboxMenuItem
with no label displayed next to the check toggle. The initial value of theCheckboxMenuItem
isfalse
. To set the label at a later time, usesetLabel()
. - public CheckboxMenuItem (String label)
- The next
CheckboxMenuItem
constructor creates aCheckboxMenuItem
withlabel
displayed next to the check toggle. The initial value of theCheckboxMenuItem
isfalse
. - public CheckboxMenuItem (String label, boolean state)
- The final
CheckboxMenuItem
constructor creates aCheckboxMenuItem
withlabel
displayed next to the check toggle. The initial value of theCheckboxMenuItem
isstate
.
- public boolean getState ()
- The
getState()
method retrieves the current state of theCheckboxMenuItem
. - public void setState (boolean condition)
- The
setState()
method changes the current state of theCheckboxMenuItem
tocondition
. Whentrue
, theCheckboxMenuItem
will have the toggle checked. - public Object[] getSelectedObjects ()
- The
getSelectedItems()
method returns the currently selected item as anObject
array. This method, which is required by theItemSelectable
interface, allows you to use the same methods to retrieve the selected items of anyCheckbox
,Choice
, orList
. The array has at most one element, which contains the label of the selected item; if no item is selected,getSelectedItems()
returnsnull
.
- public synchronized void addNotify ()
- The
addNotify()
method creates theCheckboxMenuItem
peer. - public String paramString ()
- The
paramString()
method ofCheckboxMenuItem
should be protected like otherparamString()
methods. However, it is public, so you have access to it. When you call thetoString()
method of aCheckboxMenuItem
, the defaulttoString()
method ofMenuComponent
is called. This in turn callsparamString()
which builds up the string to display. At theCheckboxMenuItem
level, the current state of the object is appended to the output. If the constructor for theCheckboxMenuItem
wasnew CheckboxMenuItem(`File`)
the results would be:
java.awt.CheckboxMenuItem[label=File,state=false]
CheckboxMenuItem Events
Event handlingA CheckboxMenuItem
generates an ACTION_EVENT
when it is selected. The argument to action()
is the label of the CheckboxMenuItem
, like the method provided by MenuItem
, not the state of the CheckboxMenuItem
as used in Checkbox
. The target of the ACTION_EVENT
is the Frame
containing the menu. You cannot subclass CheckboxMenuItem
and handle the Event
within the subclass unless you override postEvent()
. Listeners and 1.1 event handling
With the Java 1.1 event model, you register listeners, which are told when the event happens.
- public void addItemListener(ItemListener listener)
- The
addItemListener()
method registerslistener
as an object that is interested in being notified when anItemEvent
passes through theEventQueue
with thisCheckboxMenuItem
as its target. When these item events occur, thelistener.itemStateChanged()
method is called. Multiple listeners can be registered. - public void removeItemListener(ItemListener listener)
- The
removeItemListener()
method removeslistener
as a interested listener. Iflistener
is not registered, nothing happens. - protected void processEvent(AWTEvent e)
- The
processEvent()
method receives everyAWTEvent
with thisCheckboxMenuItem
as its target.processEvent()
then passes it along to any listeners for processing. When you subclassCheckboxMenuItem
, overridingprocessEvent()
allows you to process all events yourself, before sending them to any listeners. In a way, overridingprocessEvent()
is like overridingpostEvent()
using the 1.0 event model.If you override
processEvent()
, remember to callsuper.processEvent(e)
last to ensure that regular event processing can occur. If you want to process your own events, it's a good idea to callenableEvents()
to ensure that events are delivered, even in the absence of registered listeners. - protected void processItemEvent(ItemEvent e)
- The
processItemEvent()
method receives everyItemEvent
with thisCheckboxMenuItem
as its target.processItemEvent()
then passes it along to any listeners for processing. When you subclassCheckboxMenuItem
, overridingprocessItemEvent()
allows you to process all item events yourself, before sending them to any listeners. In a way, overridingprocessItemEvent()
is like overridingaction()
using the 1.0 event model.If you override
processItemEvent()
, remember to call the methodsuper.processItemEvent(e)
last to ensure that regular event processing can occur. If you want to process your own events, it's a good idea to callenableEvents()
to ensure that events are delivered even in the absence of registered listeners.