FileDocCategorySizeDatePackage
CheckboxMenuItem.javaAPI DocJava SE 6 API20799Tue Jun 10 00:25:12 BST 2008java.awt

CheckboxMenuItem

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:

Menu labeled Examples, containing items Basic, Simple, Check, and More Examples. The Check item is a CheckBoxMenuItem instance, in the off state.

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.

version
1.72, 07/11/06
author
Sami Shaio
see
java.awt.event.ItemEvent
see
java.awt.event.ItemListener
since
JDK1.0

Fields Summary
boolean
state
The state of a checkbox menu item
transient ItemListener
itemListener
private static final String
base
private static int
nameCounter
private static final long
serialVersionUID
private int
checkboxMenuItemSerializedDataVersion
Constructors Summary
public CheckboxMenuItem()
Create a check box menu item with an empty label. The item's state is initially set to "off."

exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true
see
java.awt.GraphicsEnvironment#isHeadless
since
JDK1.1


                                       
        
	this("", false);
    
public CheckboxMenuItem(String label)
Create a check box menu item with the specified label. The item's state is initially set to "off."

param
label a string label for the check box menu item, or null for an unlabeled menu item.
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true
see
java.awt.GraphicsEnvironment#isHeadless

	this(label, false);
    
public CheckboxMenuItem(String label, boolean state)
Create a check box menu item with the specified label and state.

param
label a string label for the check box menu item, or null for an unlabeled menu item.
param
state the initial state of the menu item, where true indicates "on" and false indicates "off."
exception
HeadlessException if GraphicsEnvironment.isHeadless() returns true
see
java.awt.GraphicsEnvironment#isHeadless
since
JDK1.1

        super(label);
    	this.state = state;
    
Methods Summary
public synchronized voidaddItemListener(java.awt.event.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.

param
l the item listener
see
#removeItemListener
see
#getItemListeners
see
#setState
see
java.awt.event.ItemEvent
see
java.awt.event.ItemListener
since
JDK1.1

	if (l == null) {
	    return;
	}
        itemListener = AWTEventMulticaster.add(itemListener, l);
        newEventsOnly = true;
    
public voidaddNotify()
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.

see
java.awt.Toolkit#createCheckboxMenuItem(java.awt.CheckboxMenuItem)
see
java.awt.Component#getToolkit()

        synchronized (getTreeLock()) {
	    if (peer == null)
	        peer = Toolkit.getDefaultToolkit().createCheckboxMenuItem(this);
	    super.addNotify();
	}
    
java.lang.StringconstructComponentName()
Construct a name for this MenuComponent. Called by getName() when the name is null.

        synchronized (getClass()) {
	    return base + nameCounter++;
	}
    
voiddoMenuEvent(long when, int modifiers)

        setState(!state);
        Toolkit.getEventQueue().postEvent(
            new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
                          getLabel(),
                          state ? ItemEvent.SELECTED :
                                  ItemEvent.DESELECTED));
    
booleaneventEnabled(java.awt.AWTEvent e)

        if (e.id == ItemEvent.ITEM_STATE_CHANGED) {
            if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 ||
                itemListener != null) {
                return true;
            }
            return false;
        }
        return super.eventEnabled(e);
    
public javax.accessibility.AccessibleContextgetAccessibleContext()
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.

return
an AccessibleAWTCheckboxMenuItem that serves as the AccessibleContext of this CheckboxMenuItem
since
1.3

        if (accessibleContext == null) {
            accessibleContext = new AccessibleAWTCheckboxMenuItem();
        }
        return accessibleContext;
    
public synchronized java.awt.event.ItemListener[]getItemListeners()
Returns an array of all the item listeners registered on this checkbox menuitem.

return
all of this checkbox menuitem's ItemListeners or an empty array if no item listeners are currently registered
see
#addItemListener
see
#removeItemListener
see
java.awt.event.ItemEvent
see
java.awt.event.ItemListener
since
1.4

        return (ItemListener[])(getListeners(ItemListener.class));
    
public T[]getListeners(java.lang.Class listenerType)
Returns an array of all the objects currently registered as 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.

param
listenerType the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
return
an array of all objects registered as FooListeners on this checkbox menuitem, or an empty array if no such listeners have been added
exception
ClassCastException if listenerType doesn't specify a class or interface that implements java.util.EventListener
see
#getItemListeners
since
1.3

 
	EventListener l = null; 
	if  (listenerType == ItemListener.class) { 
	    l = itemListener;
	} else {
	    return super.getListeners(listenerType);
	}
	return AWTEventMulticaster.getListeners(l, listenerType);
    
public synchronized java.lang.Object[]getSelectedObjects()
Returns the an array (length 1) containing the checkbox menu item label or null if the checkbox is not selected.

see
ItemSelectable

        if (state) {
            Object[] items = new Object[1];
            items[0] = label;
            return items;
        }
        return null;
    
public booleangetState()
Determines whether the state of this check box menu item is "on" or "off."

return
the state of this check box menu item, where true indicates "on" and false indicates "off"
see
#setState

	return state;
    
private static native voidinitIDs()
Initialize JNI field and method IDs

public java.lang.StringparamString()
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.

return
the parameter string of this check box menu item

	return super.paramString() + ",state=" + state;
    
protected voidprocessEvent(java.awt.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.

param
e the event
see
java.awt.event.ItemEvent
see
#processItemEvent
since
JDK1.1

        if (e instanceof ItemEvent) {
            processItemEvent((ItemEvent)e);
	    return;
        }
	super.processEvent(e);
    
protected voidprocessItemEvent(java.awt.event.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.

param
e the item event
see
java.awt.event.ItemEvent
see
java.awt.event.ItemListener
see
#addItemListener
see
java.awt.MenuItem#enableEvents
since
JDK1.1

        ItemListener listener = itemListener;
        if (listener != null) {
            listener.itemStateChanged(e);
        }
    
private voidreadObject(java.io.ObjectInputStream s)

      s.defaultReadObject();

      Object keyOrNull;
      while(null != (keyOrNull = s.readObject())) {
	String key = ((String)keyOrNull).intern();

	if (itemListenerK == key)
	  addItemListener((ItemListener)(s.readObject()));

	else // skip value for unrecognized key
	  s.readObject();
      }
    
public synchronized voidremoveItemListener(java.awt.event.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.

param
l the item listener
see
#addItemListener
see
#getItemListeners
see
java.awt.event.ItemEvent
see
java.awt.event.ItemListener
since
JDK1.1

	if (l == null) {
	    return;
	}
        itemListener = AWTEventMulticaster.remove(itemListener, l);
    
public synchronized voidsetState(boolean b)
Sets this check box menu item to the specifed 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.

param
b true if the check box menu item is on, otherwise false
see
#getState

	state = b;
	CheckboxMenuItemPeer peer = (CheckboxMenuItemPeer)this.peer;
	if (peer != null) {
	    peer.setState(b);
	}
    
private voidwriteObject(java.io.ObjectOutputStream s)
Writes default serializable fields to stream. Writes a list of serializable ItemListeners as optional data. The non-serializable ItemListeners are detected and no attempt is made to serialize them.

param
s the ObjectOutputStream to write
serialData
null terminated sequence of 0 or more pairs; the pair consists of a String and an Object; the String indicates the type of object and is one of the following: itemListenerK indicating an ItemListener object
see
AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
see
java.awt.Component#itemListenerK
see
#readObject(ObjectInputStream)


                                                                                                
       
       
    
      s.defaultWriteObject();

      AWTEventMulticaster.save(s, itemListenerK, itemListener);
      s.writeObject(null);