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

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  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 
  89     /**
  90      * Creates an initially unselected toggle button
  91      * without setting the text or image.
  92      */
  93     public JToggleButton () {
  94         this(null, null, false);
  95     }
  96 
  97     /**
  98      * Creates an initially unselected toggle button
  99      * with the specified image but no text.


 178         // initialize
 179         init(text, icon);
 180     }
 181 
 182     /**
 183      * Resets the UI property to a value from the current look and feel.
 184      *
 185      * @see JComponent#updateUI
 186      */
 187     public void updateUI() {
 188         setUI((ButtonUI)UIManager.getUI(this));
 189     }
 190 
 191     /**
 192      * Returns a string that specifies the name of the l&amp;f class
 193      * that renders this component.
 194      *
 195      * @return String "ToggleButtonUI"
 196      * @see JComponent#getUIClassID
 197      * @see UIDefaults#getUI
 198      * @beaninfo
 199      *  description: A string that specifies the name of the L&amp;F class
 200      */


 201     public String getUIClassID() {
 202         return uiClassID;
 203     }
 204 
 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


 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.




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  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 import java.beans.JavaBean;
  30 import java.beans.BeanProperty;
  31 

  32 import javax.swing.plaf.*;
  33 import javax.accessibility.*;
  34 
  35 import java.io.ObjectOutputStream;

  36 import java.io.IOException;
  37 

  38 /**
  39  * An implementation of a two-state button.
  40  * The <code>JRadioButton</code> and <code>JCheckBox</code> classes
  41  * are subclasses of this class.
  42  * For information on using them see
  43  * <a
  44  href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>,
  45  * a section in <em>The Java Tutorial</em>.
  46  * <p>
  47  * Buttons can be configured, and to some degree controlled, by
  48  * <code><a href="Action.html">Action</a></code>s.  Using an
  49  * <code>Action</code> with a button has many benefits beyond directly
  50  * configuring a button.  Refer to <a href="Action.html#buttonActions">
  51  * Swing Components Supporting <code>Action</code></a> for more
  52  * details, and you can find more information in <a
  53  * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
  54  * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
  55  * <p>
  56  * <strong>Warning:</strong> Swing is not thread safe. For more
  57  * information see <a
  58  * href="package-summary.html#threading">Swing's Threading
  59  * Policy</a>.
  60  * <p>
  61  * <strong>Warning:</strong>
  62  * Serialized objects of this class will not be compatible with
  63  * future Swing releases. The current serialization support is
  64  * appropriate for short term storage or RMI between applications running
  65  * the same version of Swing.  As of 1.4, support for long term storage
  66  * of all JavaBeans&trade;
  67  * has been added to the <code>java.beans</code> package.
  68  * Please see {@link java.beans.XMLEncoder}.
  69  *




  70  * @see JRadioButton
  71  * @see JCheckBox
  72  * @author Jeff Dinkins
  73  * @since 1.2
  74  */
  75 @JavaBean(defaultProperty = "UIClassID", description = "An implementation of a two-state button.")
  76 @SwingContainer(false)
  77 @SuppressWarnings("serial") // Same-version serialization only
  78 public class JToggleButton extends AbstractButton implements Accessible {
  79 
  80     /**
  81      * @see #getUIClassID
  82      * @see #readObject
  83      */
  84     private static final String uiClassID = "ToggleButtonUI";
  85 
  86     /**
  87      * Creates an initially unselected toggle button
  88      * without setting the text or image.
  89      */
  90     public JToggleButton () {
  91         this(null, null, false);
  92     }
  93 
  94     /**
  95      * Creates an initially unselected toggle button
  96      * with the specified image but no text.


 175         // initialize
 176         init(text, icon);
 177     }
 178 
 179     /**
 180      * Resets the UI property to a value from the current look and feel.
 181      *
 182      * @see JComponent#updateUI
 183      */
 184     public void updateUI() {
 185         setUI((ButtonUI)UIManager.getUI(this));
 186     }
 187 
 188     /**
 189      * Returns a string that specifies the name of the l&amp;f class
 190      * that renders this component.
 191      *
 192      * @return String "ToggleButtonUI"
 193      * @see JComponent#getUIClassID
 194      * @see UIDefaults#getUI


 195      */
 196     @BeanProperty(bound = false, description
 197             = "A string that specifies the name of the L&amp;F class")
 198     public String getUIClassID() {
 199         return uiClassID;
 200     }
 201 
 202 
 203     /**
 204      * Overriden to return true, JToggleButton supports
 205      * the selected state.
 206      */
 207     boolean shouldUpdateSelectedStateFromAction() {
 208         return true;
 209     }
 210 
 211     // *********************************************************************
 212 
 213     /**
 214      * The ToggleButton model
 215      * <p>
 216      * <strong>Warning:</strong>
 217      * Serialized objects of this class will not be compatible with


 342      *
 343      * @return  a string representation of this JToggleButton.
 344      */
 345     protected String paramString() {
 346         return super.paramString();
 347     }
 348 
 349 
 350 /////////////////
 351 // Accessibility support
 352 ////////////////
 353 
 354     /**
 355      * Gets the AccessibleContext associated with this JToggleButton.
 356      * For toggle buttons, the AccessibleContext takes the form of an
 357      * AccessibleJToggleButton.
 358      * A new AccessibleJToggleButton instance is created if necessary.
 359      *
 360      * @return an AccessibleJToggleButton that serves as the
 361      *         AccessibleContext of this JToggleButton



 362      */
 363     @BeanProperty(bound = false, expert = true, description
 364             = "The AccessibleContext associated with this ToggleButton.")
 365     public AccessibleContext getAccessibleContext() {
 366         if (accessibleContext == null) {
 367             accessibleContext = new AccessibleJToggleButton();
 368         }
 369         return accessibleContext;
 370     }
 371 
 372     /**
 373      * This class implements accessibility support for the
 374      * <code>JToggleButton</code> class.  It provides an implementation of the
 375      * Java Accessibility API appropriate to toggle button user-interface
 376      * elements.
 377      * <p>
 378      * <strong>Warning:</strong>
 379      * Serialized objects of this class will not be compatible with
 380      * future Swing releases. The current serialization support is
 381      * appropriate for short term storage or RMI between applications running
 382      * the same version of Swing.  As of 1.4, support for long term storage
 383      * of all JavaBeans&trade;
 384      * has been added to the <code>java.beans</code> package.