< prev index next >

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

Print this page




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 
  30 import javax.swing.event.*;
  31 import javax.swing.plaf.*;
  32 import javax.accessibility.*;
  33 
  34 import java.io.ObjectOutputStream;
  35 import java.io.ObjectInputStream;
  36 import java.io.IOException;
  37 
  38 
  39 /**
  40  * An implementation of a two-state button.
  41  * The <code>JRadioButton</code> and <code>JCheckBox</code> classes
  42  * are subclasses of this class.
  43  * For information on using them see
  44  * <a
  45  href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>,
  46  * a section in <em>The Java Tutorial</em>.
  47  * <p>
  48  * Buttons can be configured, and to some degree controlled, by
  49  * <code><a href="Action.html">Action</a></code>s.  Using an
  50  * <code>Action</code> with a button has many benefits beyond directly
  51  * configuring a button.  Refer to <a href="Action.html#buttonActions">
  52  * Swing Components Supporting <code>Action</code></a> for more
  53  * details, and you can find more information in <a
  54  * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
  55  * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
  56  * <p>
  57  * <strong>Warning:</strong> Swing is not thread safe. For more
  58  * information see <a
  59  * href="package-summary.html#threading">Swing's Threading
  60  * Policy</a>.
  61  * <p>
  62  * <strong>Warning:</strong>
  63  * Serialized objects of this class will not be compatible with
  64  * future Swing releases. The current serialization support is
  65  * appropriate for short term storage or RMI between applications running
  66  * the same version of Swing.  As of 1.4, support for long term storage
  67  * of all JavaBeans&trade;
  68  * has been added to the <code>java.beans</code> package.
  69  * Please see {@link java.beans.XMLEncoder}.
  70  *
  71  * @beaninfo
  72  *   attribute: isContainer false
  73  * description: An implementation of a two-state button.
  74  *
  75  * @see JRadioButton
  76  * @see JCheckBox
  77  * @author Jeff Dinkins
  78  * @since 1.2
  79  */
  80 @SuppressWarnings("serial") // Same-version serialization only
  81 public class JToggleButton extends AbstractButton implements Accessible {
  82 
  83     /**
  84      * @see #getUIClassID
  85      * @see #readObject
  86      */
  87     private static final String uiClassID = "ToggleButtonUI";
  88 


 205 
 206     /**
 207      * Overriden to return true, JToggleButton supports
 208      * the selected state.
 209      */
 210     boolean shouldUpdateSelectedStateFromAction() {
 211         return true;
 212     }
 213 
 214     // *********************************************************************
 215 
 216     /**
 217      * The ToggleButton model
 218      * <p>
 219      * <strong>Warning:</strong>
 220      * Serialized objects of this class will not be compatible with
 221      * future Swing releases. The current serialization support is
 222      * appropriate for short term storage or RMI between applications running
 223      * the same version of Swing.  As of 1.4, support for long term storage
 224      * of all JavaBeans&trade;
 225      * has been added to the <code>java.beans</code> package.
 226      * Please see {@link java.beans.XMLEncoder}.
 227      */
 228     @SuppressWarnings("serial") // Same-version serialization only
 229     public static class ToggleButtonModel extends DefaultButtonModel {
 230 
 231         /**
 232          * Creates a new ToggleButton Model
 233          */
 234         public ToggleButtonModel () {
 235         }
 236 
 237         /**
 238          * Checks if the button is selected.
 239          */
 240         public boolean isSelected() {
 241 //              if(getGroup() != null) {
 242 //                  return getGroup().isSelected(this);
 243 //              } else {
 244                 return (stateMask & SELECTED) != 0;
 245 //              }


 324      * See readObject() and writeObject() in JComponent for more
 325      * information about serialization in Swing.
 326      */
 327     private void writeObject(ObjectOutputStream s) throws IOException {
 328         s.defaultWriteObject();
 329         if (getUIClassID().equals(uiClassID)) {
 330             byte count = JComponent.getWriteObjCounter(this);
 331             JComponent.setWriteObjCounter(this, --count);
 332             if (count == 0 && ui != null) {
 333                 ui.installUI(this);
 334             }
 335         }
 336     }
 337 
 338 
 339     /**
 340      * Returns a string representation of this JToggleButton. This method
 341      * is intended to be used only for debugging purposes, and the
 342      * content and format of the returned string may vary between
 343      * implementations. The returned string may be empty but may not
 344      * be <code>null</code>.
 345      *
 346      * @return  a string representation of this JToggleButton.
 347      */
 348     protected String paramString() {
 349         return super.paramString();
 350     }
 351 
 352 
 353 /////////////////
 354 // Accessibility support
 355 ////////////////
 356 
 357     /**
 358      * Gets the AccessibleContext associated with this JToggleButton.
 359      * For toggle buttons, the AccessibleContext takes the form of an
 360      * AccessibleJToggleButton.
 361      * A new AccessibleJToggleButton instance is created if necessary.
 362      *
 363      * @return an AccessibleJToggleButton that serves as the
 364      *         AccessibleContext of this JToggleButton
 365      * @beaninfo
 366      *       expert: true
 367      *  description: The AccessibleContext associated with this ToggleButton.
 368      */
 369     public AccessibleContext getAccessibleContext() {
 370         if (accessibleContext == null) {
 371             accessibleContext = new AccessibleJToggleButton();
 372         }
 373         return accessibleContext;
 374     }
 375 
 376     /**
 377      * This class implements accessibility support for the
 378      * <code>JToggleButton</code> class.  It provides an implementation of the
 379      * Java Accessibility API appropriate to toggle button user-interface
 380      * elements.
 381      * <p>
 382      * <strong>Warning:</strong>
 383      * Serialized objects of this class will not be compatible with
 384      * future Swing releases. The current serialization support is
 385      * appropriate for short term storage or RMI between applications running
 386      * the same version of Swing.  As of 1.4, support for long term storage
 387      * of all JavaBeans&trade;
 388      * has been added to the <code>java.beans</code> package.
 389      * Please see {@link java.beans.XMLEncoder}.
 390      */
 391     @SuppressWarnings("serial") // Same-version serialization only
 392     protected class AccessibleJToggleButton extends AccessibleAbstractButton
 393             implements ItemListener {
 394 
 395         /**
 396          * Constructs {@code AccessibleJToggleButton}
 397          */
 398         public AccessibleJToggleButton() {
 399             super();
 400             JToggleButton.this.addItemListener(this);
 401         }
 402 
 403         /**
 404          * Fire accessible property change events when the state of the
 405          * toggle button changes.
 406          */
 407         public void itemStateChanged(ItemEvent e) {
 408             JToggleButton tb = (JToggleButton) e.getSource();




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 
  30 import javax.swing.event.*;
  31 import javax.swing.plaf.*;
  32 import javax.accessibility.*;
  33 
  34 import java.io.ObjectOutputStream;
  35 import java.io.ObjectInputStream;
  36 import java.io.IOException;
  37 
  38 
  39 /**
  40  * An implementation of a two-state button.
  41  * The {@code JRadioButton} and {@code JCheckBox} classes
  42  * are subclasses of this class.
  43  * For information on using them see
  44  * <a
  45  href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>,
  46  * a section in <em>The Java Tutorial</em>.
  47  * <p>
  48  * Buttons can be configured, and to some degree controlled, by
  49  * <code><a href="Action.html">Action</a></code>s.  Using an
  50  * {@code Action} with a button has many benefits beyond directly
  51  * configuring a button.  Refer to <a href="Action.html#buttonActions">
  52  * Swing Components Supporting {@code Action}</a> for more
  53  * details, and you can find more information in <a
  54  * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
  55  * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
  56  * <p>
  57  * <strong>Warning:</strong> Swing is not thread safe. For more
  58  * information see <a
  59  * href="package-summary.html#threading">Swing's Threading
  60  * Policy</a>.
  61  * <p>
  62  * <strong>Warning:</strong>
  63  * Serialized objects of this class will not be compatible with
  64  * future Swing releases. The current serialization support is
  65  * appropriate for short term storage or RMI between applications running
  66  * the same version of Swing.  As of 1.4, support for long term storage
  67  * of all JavaBeans&trade;
  68  * has been added to the {@code java.beans} package.
  69  * Please see {@link java.beans.XMLEncoder}.
  70  *
  71  * @beaninfo
  72  *   attribute: isContainer false
  73  * description: An implementation of a two-state button.
  74  *
  75  * @see JRadioButton
  76  * @see JCheckBox
  77  * @author Jeff Dinkins
  78  * @since 1.2
  79  */
  80 @SuppressWarnings("serial") // Same-version serialization only
  81 public class JToggleButton extends AbstractButton implements Accessible {
  82 
  83     /**
  84      * @see #getUIClassID
  85      * @see #readObject
  86      */
  87     private static final String uiClassID = "ToggleButtonUI";
  88 


 205 
 206     /**
 207      * Overriden to return true, JToggleButton supports
 208      * the selected state.
 209      */
 210     boolean shouldUpdateSelectedStateFromAction() {
 211         return true;
 212     }
 213 
 214     // *********************************************************************
 215 
 216     /**
 217      * The ToggleButton model
 218      * <p>
 219      * <strong>Warning:</strong>
 220      * Serialized objects of this class will not be compatible with
 221      * future Swing releases. The current serialization support is
 222      * appropriate for short term storage or RMI between applications running
 223      * the same version of Swing.  As of 1.4, support for long term storage
 224      * of all JavaBeans&trade;
 225      * has been added to the {@code java.beans} package.
 226      * Please see {@link java.beans.XMLEncoder}.
 227      */
 228     @SuppressWarnings("serial") // Same-version serialization only
 229     public static class ToggleButtonModel extends DefaultButtonModel {
 230 
 231         /**
 232          * Creates a new ToggleButton Model
 233          */
 234         public ToggleButtonModel () {
 235         }
 236 
 237         /**
 238          * Checks if the button is selected.
 239          */
 240         public boolean isSelected() {
 241 //              if(getGroup() != null) {
 242 //                  return getGroup().isSelected(this);
 243 //              } else {
 244                 return (stateMask & SELECTED) != 0;
 245 //              }


 324      * See readObject() and writeObject() in JComponent for more
 325      * information about serialization in Swing.
 326      */
 327     private void writeObject(ObjectOutputStream s) throws IOException {
 328         s.defaultWriteObject();
 329         if (getUIClassID().equals(uiClassID)) {
 330             byte count = JComponent.getWriteObjCounter(this);
 331             JComponent.setWriteObjCounter(this, --count);
 332             if (count == 0 && ui != null) {
 333                 ui.installUI(this);
 334             }
 335         }
 336     }
 337 
 338 
 339     /**
 340      * Returns a string representation of this JToggleButton. This method
 341      * is intended to be used only for debugging purposes, and the
 342      * content and format of the returned string may vary between
 343      * implementations. The returned string may be empty but may not
 344      * be {@code null}.
 345      *
 346      * @return  a string representation of this JToggleButton.
 347      */
 348     protected String paramString() {
 349         return super.paramString();
 350     }
 351 
 352 
 353 /////////////////
 354 // Accessibility support
 355 ////////////////
 356 
 357     /**
 358      * Gets the AccessibleContext associated with this JToggleButton.
 359      * For toggle buttons, the AccessibleContext takes the form of an
 360      * AccessibleJToggleButton.
 361      * A new AccessibleJToggleButton instance is created if necessary.
 362      *
 363      * @return an AccessibleJToggleButton that serves as the
 364      *         AccessibleContext of this JToggleButton
 365      * @beaninfo
 366      *       expert: true
 367      *  description: The AccessibleContext associated with this ToggleButton.
 368      */
 369     public AccessibleContext getAccessibleContext() {
 370         if (accessibleContext == null) {
 371             accessibleContext = new AccessibleJToggleButton();
 372         }
 373         return accessibleContext;
 374     }
 375 
 376     /**
 377      * This class implements accessibility support for the
 378      * {@code JToggleButton} class.  It provides an implementation of the
 379      * Java Accessibility API appropriate to toggle button user-interface
 380      * elements.
 381      * <p>
 382      * <strong>Warning:</strong>
 383      * Serialized objects of this class will not be compatible with
 384      * future Swing releases. The current serialization support is
 385      * appropriate for short term storage or RMI between applications running
 386      * the same version of Swing.  As of 1.4, support for long term storage
 387      * of all JavaBeans&trade;
 388      * has been added to the {@code java.beans} package.
 389      * Please see {@link java.beans.XMLEncoder}.
 390      */
 391     @SuppressWarnings("serial") // Same-version serialization only
 392     protected class AccessibleJToggleButton extends AccessibleAbstractButton
 393             implements ItemListener {
 394 
 395         /**
 396          * Constructs {@code AccessibleJToggleButton}
 397          */
 398         public AccessibleJToggleButton() {
 399             super();
 400             JToggleButton.this.addItemListener(this);
 401         }
 402 
 403         /**
 404          * Fire accessible property change events when the state of the
 405          * toggle button changes.
 406          */
 407         public void itemStateChanged(ItemEvent e) {
 408             JToggleButton tb = (JToggleButton) e.getSource();


< prev index next >