< prev index next >

src/java.desktop/share/classes/javax/swing/JCheckBoxMenuItem.java

Print this page




  28 
  29 import java.awt.*;
  30 import java.awt.event.*;
  31 import java.awt.image.*;
  32 
  33 import java.io.ObjectOutputStream;
  34 import java.io.ObjectInputStream;
  35 import java.io.IOException;
  36 
  37 import javax.swing.plaf.*;
  38 import javax.accessibility.*;
  39 
  40 
  41 /**
  42  * A menu item that can be selected or deselected. If selected, the menu
  43  * item typically appears with a checkmark next to it. If unselected or
  44  * deselected, the menu item appears without a checkmark. Like a regular
  45  * menu item, a check box menu item can have either text or a graphic
  46  * icon associated with it, or both.
  47  * <p>
  48  * Either <code>isSelected</code>/<code>setSelected</code> or
  49  * <code>getState</code>/<code>setState</code> can be used
  50  * to determine/specify the menu item's selection state. The
  51  * preferred methods are <code>isSelected</code> and
  52  * <code>setSelected</code>, which work for all menus and buttons.
  53  * The <code>getState</code> and <code>setState</code> methods exist for
  54  * compatibility with other component sets.
  55  * <p>
  56  * Menu items can be configured, and to some degree controlled, by
  57  * <code><a href="Action.html">Action</a></code>s.  Using an
  58  * <code>Action</code> with a menu item has many benefits beyond directly
  59  * configuring a menu item.  Refer to <a href="Action.html#buttonActions">
  60  * Swing Components Supporting <code>Action</code></a> for more
  61  * details, and you can find more information in <a
  62  * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
  63  * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
  64  * <p>
  65  * For further information and examples of using check box menu items,
  66  * see <a
  67  href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
  68  * a section in <em>The Java Tutorial.</em>
  69  * <p>
  70  * <strong>Warning:</strong> Swing is not thread safe. For more
  71  * information see <a
  72  * href="package-summary.html#threading">Swing's Threading
  73  * Policy</a>.
  74  * <p>
  75  * <strong>Warning:</strong>
  76  * Serialized objects of this class will not be compatible with
  77  * future Swing releases. The current serialization support is
  78  * appropriate for short term storage or RMI between applications running
  79  * the same version of Swing.  As of 1.4, support for long term storage
  80  * of all JavaBeans&trade;
  81  * has been added to the <code>java.beans</code> package.
  82  * Please see {@link java.beans.XMLEncoder}.
  83  *
  84  * @beaninfo
  85  *   attribute: isContainer false
  86  * description: A menu item which can be selected or deselected.
  87  *
  88  * @author Georges Saab
  89  * @author David Karlton
  90  * @since 1.2
  91  */
  92 @SuppressWarnings("serial") // Same-version serialization only
  93 public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
  94         Accessible
  95 {
  96     /**
  97      * @see #getUIClassID
  98      * @see #readObject
  99      */
 100     private static final String uiClassID = "CheckBoxMenuItemUI";
 101 


 228      * See readObject() and writeObject() in JComponent for more
 229      * information about serialization in Swing.
 230      */
 231     private void writeObject(ObjectOutputStream s) throws IOException {
 232         s.defaultWriteObject();
 233         if (getUIClassID().equals(uiClassID)) {
 234             byte count = JComponent.getWriteObjCounter(this);
 235             JComponent.setWriteObjCounter(this, --count);
 236             if (count == 0 && ui != null) {
 237                 ui.installUI(this);
 238             }
 239         }
 240     }
 241 
 242 
 243     /**
 244      * Returns a string representation of this JCheckBoxMenuItem. This method
 245      * is intended to be used only for debugging purposes, and the
 246      * content and format of the returned string may vary between
 247      * implementations. The returned string may be empty but may not
 248      * be <code>null</code>.
 249      *
 250      * @return  a string representation of this JCheckBoxMenuItem.
 251      */
 252     protected String paramString() {
 253         return super.paramString();
 254     }
 255 
 256     /**
 257      * Overriden to return true, JCheckBoxMenuItem supports
 258      * the selected state.
 259      */
 260     boolean shouldUpdateSelectedStateFromAction() {
 261         return true;
 262     }
 263 
 264 /////////////////
 265 // Accessibility support
 266 ////////////////
 267 
 268     /**
 269      * Gets the AccessibleContext associated with this JCheckBoxMenuItem.
 270      * For JCheckBoxMenuItems, the AccessibleContext takes the form of an
 271      * AccessibleJCheckBoxMenuItem.
 272      * A new AccessibleJCheckBoxMenuItem instance is created if necessary.
 273      *
 274      * @return an AccessibleJCheckBoxMenuItem that serves as the
 275      *         AccessibleContext of this AccessibleJCheckBoxMenuItem
 276      */
 277     public AccessibleContext getAccessibleContext() {
 278         if (accessibleContext == null) {
 279             accessibleContext = new AccessibleJCheckBoxMenuItem();
 280         }
 281         return accessibleContext;
 282     }
 283 
 284     /**
 285      * This class implements accessibility support for the
 286      * <code>JCheckBoxMenuItem</code> class.  It provides an implementation
 287      * of the Java Accessibility API appropriate to checkbox menu item
 288      * user-interface elements.
 289      * <p>
 290      * <strong>Warning:</strong>
 291      * Serialized objects of this class will not be compatible with
 292      * future Swing releases. The current serialization support is
 293      * appropriate for short term storage or RMI between applications running
 294      * the same version of Swing.  As of 1.4, support for long term storage
 295      * of all JavaBeans&trade;
 296      * has been added to the <code>java.beans</code> package.
 297      * Please see {@link java.beans.XMLEncoder}.
 298      */
 299     @SuppressWarnings("serial") // Same-version serialization only
 300     protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem {
 301         /**
 302          * Get the role of this object.
 303          *
 304          * @return an instance of AccessibleRole describing the role of the
 305          * object
 306          */
 307         public AccessibleRole getAccessibleRole() {
 308             return AccessibleRole.CHECK_BOX;
 309         }
 310     } // inner class AccessibleJCheckBoxMenuItem
 311 }


  28 
  29 import java.awt.*;
  30 import java.awt.event.*;
  31 import java.awt.image.*;
  32 
  33 import java.io.ObjectOutputStream;
  34 import java.io.ObjectInputStream;
  35 import java.io.IOException;
  36 
  37 import javax.swing.plaf.*;
  38 import javax.accessibility.*;
  39 
  40 
  41 /**
  42  * A menu item that can be selected or deselected. If selected, the menu
  43  * item typically appears with a checkmark next to it. If unselected or
  44  * deselected, the menu item appears without a checkmark. Like a regular
  45  * menu item, a check box menu item can have either text or a graphic
  46  * icon associated with it, or both.
  47  * <p>
  48  * Either {@code isSelected}/{@code setSelected} or
  49  * {@code getState}/{@code setState} can be used
  50  * to determine/specify the menu item's selection state. The
  51  * preferred methods are {@code isSelected} and
  52  * {@code setSelected}, which work for all menus and buttons.
  53  * The {@code getState} and {@code setState} methods exist for
  54  * compatibility with other component sets.
  55  * <p>
  56  * Menu items can be configured, and to some degree controlled, by
  57  * <code><a href="Action.html">Action</a></code>s.  Using an
  58  * {@code Action} with a menu item has many benefits beyond directly
  59  * configuring a menu item.  Refer to <a href="Action.html#buttonActions">
  60  * Swing Components Supporting {@code Action}</a> for more
  61  * details, and you can find more information in <a
  62  * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
  63  * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
  64  * <p>
  65  * For further information and examples of using check box menu items,
  66  * see <a
  67  href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
  68  * a section in <em>The Java Tutorial.</em>
  69  * <p>
  70  * <strong>Warning:</strong> Swing is not thread safe. For more
  71  * information see <a
  72  * href="package-summary.html#threading">Swing's Threading
  73  * Policy</a>.
  74  * <p>
  75  * <strong>Warning:</strong>
  76  * Serialized objects of this class will not be compatible with
  77  * future Swing releases. The current serialization support is
  78  * appropriate for short term storage or RMI between applications running
  79  * the same version of Swing.  As of 1.4, support for long term storage
  80  * of all JavaBeans&trade;
  81  * has been added to the {@code java.beans} package.
  82  * Please see {@link java.beans.XMLEncoder}.
  83  *
  84  * @beaninfo
  85  *   attribute: isContainer false
  86  * description: A menu item which can be selected or deselected.
  87  *
  88  * @author Georges Saab
  89  * @author David Karlton
  90  * @since 1.2
  91  */
  92 @SuppressWarnings("serial") // Same-version serialization only
  93 public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
  94         Accessible
  95 {
  96     /**
  97      * @see #getUIClassID
  98      * @see #readObject
  99      */
 100     private static final String uiClassID = "CheckBoxMenuItemUI";
 101 


 228      * See readObject() and writeObject() in JComponent for more
 229      * information about serialization in Swing.
 230      */
 231     private void writeObject(ObjectOutputStream s) throws IOException {
 232         s.defaultWriteObject();
 233         if (getUIClassID().equals(uiClassID)) {
 234             byte count = JComponent.getWriteObjCounter(this);
 235             JComponent.setWriteObjCounter(this, --count);
 236             if (count == 0 && ui != null) {
 237                 ui.installUI(this);
 238             }
 239         }
 240     }
 241 
 242 
 243     /**
 244      * Returns a string representation of this JCheckBoxMenuItem. This method
 245      * is intended to be used only for debugging purposes, and the
 246      * content and format of the returned string may vary between
 247      * implementations. The returned string may be empty but may not
 248      * be {@code null}.
 249      *
 250      * @return  a string representation of this JCheckBoxMenuItem.
 251      */
 252     protected String paramString() {
 253         return super.paramString();
 254     }
 255 
 256     /**
 257      * Overriden to return true, JCheckBoxMenuItem supports
 258      * the selected state.
 259      */
 260     boolean shouldUpdateSelectedStateFromAction() {
 261         return true;
 262     }
 263 
 264 /////////////////
 265 // Accessibility support
 266 ////////////////
 267 
 268     /**
 269      * Gets the AccessibleContext associated with this JCheckBoxMenuItem.
 270      * For JCheckBoxMenuItems, the AccessibleContext takes the form of an
 271      * AccessibleJCheckBoxMenuItem.
 272      * A new AccessibleJCheckBoxMenuItem instance is created if necessary.
 273      *
 274      * @return an AccessibleJCheckBoxMenuItem that serves as the
 275      *         AccessibleContext of this AccessibleJCheckBoxMenuItem
 276      */
 277     public AccessibleContext getAccessibleContext() {
 278         if (accessibleContext == null) {
 279             accessibleContext = new AccessibleJCheckBoxMenuItem();
 280         }
 281         return accessibleContext;
 282     }
 283 
 284     /**
 285      * This class implements accessibility support for the
 286      * {@code JCheckBoxMenuItem} class.  It provides an implementation
 287      * of the Java Accessibility API appropriate to checkbox menu item
 288      * user-interface elements.
 289      * <p>
 290      * <strong>Warning:</strong>
 291      * Serialized objects of this class will not be compatible with
 292      * future Swing releases. The current serialization support is
 293      * appropriate for short term storage or RMI between applications running
 294      * the same version of Swing.  As of 1.4, support for long term storage
 295      * of all JavaBeans&trade;
 296      * has been added to the {@code java.beans} package.
 297      * Please see {@link java.beans.XMLEncoder}.
 298      */
 299     @SuppressWarnings("serial") // Same-version serialization only
 300     protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem {
 301         /**
 302          * Get the role of this object.
 303          *
 304          * @return an instance of AccessibleRole describing the role of the
 305          * object
 306          */
 307         public AccessibleRole getAccessibleRole() {
 308             return AccessibleRole.CHECK_BOX;
 309         }
 310     } // inner class AccessibleJCheckBoxMenuItem
 311 }
< prev index next >