Module java.desktop
Package java.awt

Class CheckboxMenuItem

  • All Implemented Interfaces:
    ItemSelectable,Serializable,Accessible


    public classCheckboxMenuItemextendsMenuItemimplementsItemSelectable,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 ofCheckBoxMenuItem:

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

    The item labeledCheck 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 ofItemEvent, theprocessEvent method examines the event and passes it along toprocessItemEvent. The latter method redirects the event to anyItemListener objects that have registered an interest in item events generated by this menu item.

    Since:
    1.0
    See Also:
    ItemEvent,ItemListener,Serialized Form
    • Constructor Detail

      • CheckboxMenuItem

        public CheckboxMenuItem(String label)                 throwsHeadlessException
        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, ornull for an unlabeled menu item.
        Throws:
        HeadlessException - if GraphicsEnvironment.isHeadless() returns true
        See Also:
        GraphicsEnvironment.isHeadless()
      • CheckboxMenuItem

        public CheckboxMenuItem(String label,                        boolean state)                 throwsHeadlessException
        Create a check box menu item with the specified label and state.
        Parameters:
        label - a string label for the check box menu item, ornull for an unlabeled menu item.
        state - the initial state of the menu item, wheretrue indicates "on" andfalse indicates "off."
        Throws:
        HeadlessException - if GraphicsEnvironment.isHeadless() returns true
        Since:
        1.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 in class MenuItem
        See Also:
        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(boolean)
      • setState

        public void setState(boolean b)
        Sets this check box menu item to the specified state. The boolean valuetrue 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 willnot trigger anItemEvent. The only way to trigger anItemEvent is by user interaction.

        Parameters:
        b -true if the check box menu item is on, otherwisefalse
        See Also:
        getState()
      • 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 in interface ItemSelectable
        Returns:
        the list of selected objects, ornull
        See Also:
        ItemSelectable
      • getListeners

        public <T extendsEventListener> T[] getListeners(Class<T> listenerType)
        Returns an array of all the objects currently registered asFooListeners upon thisCheckboxMenuItem.FooListeners are registered using theaddFooListener method.

        You can specify thelistenerType argument with a class literal, such asFooListener.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 in class MenuItem
        Type Parameters:
        T - the type of the listeners
        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 asFooListeners on this checkbox menuitem, or an empty array if no such listeners have been added
        Throws:
        ClassCastException - iflistenerType doesn't specify a class or interface that implementsjava.util.EventListener
        Since:
        1.3
        See Also:
        getItemListeners()
      • processEvent

        protected void processEvent(AWTEvent e)
        Processes events on this check box menu item. If the event is an instance ofItemEvent, this method invokes theprocessItemEvent method. If the event is not an item event, it invokesprocessEvent on the superclass.

        Check box menu items currently support only item events.

        Note that if the event parameter isnull the behavior is unspecified and may result in an exception.

        Overrides:
        processEvent in class MenuItem
        Parameters:
        e - the event
        Since:
        1.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 registeredItemListener 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:

        • AnItemListener object is registered viaaddItemListener.
        • Item events are enabled viaenableEvents.

        Note that if the event parameter isnull the behavior is unspecified and may result in an exception.

        Parameters:
        e - the item event
        Since:
        1.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 benull.
        Overrides:
        paramString in class MenuItem
        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 in interface Accessible
        Overrides:
        getAccessibleContext in class MenuItem
        Returns:
        an AccessibleAWTCheckboxMenuItem that serves as the AccessibleContext of this CheckboxMenuItem
        Since:
        1.3