FileDocCategorySizeDatePackage
JCheckBoxMenuItem.javaAPI DocJava SE 5 API8752Fri Aug 26 14:57:54 BST 2005javax.swing

JCheckBoxMenuItem

public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants, Accessible
A menu item that can be selected or deselected. If selected, the menu item typically appears with a checkmark next to it. If unselected or deselected, the menu item appears without a checkmark. Like a regular menu item, a check box menu item can have either text or a graphic icon associated with it, or both.

Either isSelected/setSelected or getState/setState can be used to determine/specify the menu item's selection state. The preferred methods are isSelected and setSelected, which work for all menus and buttons. The getState and setState methods exist for compatibility with other component sets.

For further information and examples of using check box menu items, see How to Use Menus, a section in The Java Tutorial.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

beaninfo
attribute: isContainer false description: A menu item which can be selected or deselected.
version
1.55 12/19/03
author
Georges Saab
author
David Karlton

Fields Summary
private static final String
uiClassID
Constructors Summary
public JCheckBoxMenuItem()
Creates an initially unselected check box menu item with no set text or icon.


                       
      
        this(null, null, false);
    
public JCheckBoxMenuItem(Icon icon)
Creates an initially unselected check box menu item with an icon.

param
icon the icon of the CheckBoxMenuItem.

        this(null, icon, false);
    
public JCheckBoxMenuItem(String text)
Creates an initially unselected check box menu item with text.

param
text the text of the CheckBoxMenuItem

        this(text, null, false);
    
public JCheckBoxMenuItem(Action a)
Creates a menu item whose properties are taken from the Action supplied.

since
1.3

        this();
	setAction(a);
    
public JCheckBoxMenuItem(String text, Icon icon)
Creates an initially unselected check box menu item with the specified text and icon.

param
text the text of the CheckBoxMenuItem
param
icon the icon of the CheckBoxMenuItem

	this(text, icon, false);
    
public JCheckBoxMenuItem(String text, boolean b)
Creates a check box menu item with the specified text and selection state.

param
text the text of the check box menu item.
param
b the selected state of the check box menu item

        this(text, null, b);
    
public JCheckBoxMenuItem(String text, Icon icon, boolean b)
Creates a check box menu item with the specified text, icon, and selection state.

param
text the text of the check box menu item
param
icon the icon of the check box menu item
param
b the selected state of the check box menu item

	super(text, icon);
        setModel(new JToggleButton.ToggleButtonModel());
        setSelected(b);
	setFocusable(false);
    
Methods Summary
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this JCheckBoxMenuItem. For JCheckBoxMenuItems, the AccessibleContext takes the form of an AccessibleJCheckBoxMenuItem. A new AccessibleJCheckBoxMenuItem instance is created if necessary.

return
an AccessibleJCheckBoxMenuItem that serves as the AccessibleContext of this AccessibleJCheckBoxMenuItem

        if (accessibleContext == null) {
            accessibleContext = new AccessibleJCheckBoxMenuItem();
        }
        return accessibleContext;
    
public java.lang.Object[]getSelectedObjects()
Returns an array (length 1) containing the check box menu item label or null if the check box is not selected.

return
an array containing one Object -- the text of the menu item -- if the item is selected; otherwise null

        if (isSelected() == false)
            return null;
        Object[] selectedObjects = new Object[1];
        selectedObjects[0] = getText();
        return selectedObjects;
    
public booleangetState()
Returns the selected-state of the item. This method exists for AWT compatibility only. New code should use isSelected() instead.

return
true if the item is selected

        return isSelected();
    
public java.lang.StringgetUIClassID()
Returns the name of the L&F class that renders this component.

return
"CheckBoxMenuItemUI"
see
JComponent#getUIClassID
see
UIDefaults#getUI

        return uiClassID;
    
protected java.lang.StringparamString()
Returns a string representation of this JCheckBoxMenuItem. 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
a string representation of this JCheckBoxMenuItem.

	return super.paramString();
    
public synchronized voidsetState(boolean b)
Sets the selected-state of the item. This method exists for AWT compatibility only. New code should use setSelected() instead.

param
b a boolean value indicating the item's selected-state, where true=selected
beaninfo
description: The selection state of the check box menu item hidden: true

        setSelected(b);
    
private voidwriteObject(java.io.ObjectOutputStream s)
See readObject() and writeObject() in JComponent for more information about serialization in Swing.

        s.defaultWriteObject();
        if (getUIClassID().equals(uiClassID)) {
            byte count = JComponent.getWriteObjCounter(this);
            JComponent.setWriteObjCounter(this, --count);
            if (count == 0 && ui != null) {
                ui.installUI(this);
            }
        }