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

Print this page




   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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.*;
  30 
  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 radio button -- an item that can be selected or
  41  * deselected, and which displays its state to the user.
  42  * Used with a {@link ButtonGroup} object to create a group of buttons
  43  * in which only one button at a time can be selected. (Create a ButtonGroup
  44  * object and use its <code>add</code> method to include the JRadioButton objects
  45  * in the group.)
  46  * <blockquote>
  47  * <strong>Note:</strong>
  48  * The ButtonGroup object is a logical grouping -- not a physical grouping.
  49  * To create a button panel, you should still create a {@link JPanel} or similar
  50  * container-object and add a {@link javax.swing.border.Border} to it to set it off from surrounding
  51  * components.
  52  * </blockquote>
  53  * <p>
  54  * Buttons can be configured, and to some degree controlled, by
  55  * <code><a href="Action.html">Action</a></code>s.  Using an
  56  * <code>Action</code> with a button has many benefits beyond directly
  57  * configuring a button.  Refer to <a href="Action.html#buttonActions">
  58  * Swing Components Supporting <code>Action</code></a> for more


  61  * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
  62  * <p>
  63  * See <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
  64  * in <em>The Java Tutorial</em>
  65  * for further documentation.
  66  * <p>
  67  * <strong>Warning:</strong> Swing is not thread safe. For more
  68  * information see <a
  69  * href="package-summary.html#threading">Swing's Threading
  70  * Policy</a>.
  71  * <p>
  72  * <strong>Warning:</strong>
  73  * Serialized objects of this class will not be compatible with
  74  * future Swing releases. The current serialization support is
  75  * appropriate for short term storage or RMI between applications running
  76  * the same version of Swing.  As of 1.4, support for long term storage
  77  * of all JavaBeans&trade;
  78  * has been added to the <code>java.beans</code> package.
  79  * Please see {@link java.beans.XMLEncoder}.
  80  *
  81  * @beaninfo
  82  *   attribute: isContainer false
  83  * description: A component which can display it's state as selected or deselected.
  84  *
  85  * @see ButtonGroup
  86  * @see JCheckBox
  87  * @author Jeff Dinkins
  88  * @since 1.2
  89  */


  90 @SuppressWarnings("serial") // Same-version serialization only
  91 public class JRadioButton extends JToggleButton implements Accessible {
  92 
  93     /**
  94      * @see #getUIClassID
  95      * @see #readObject
  96      */
  97     private static final String uiClassID = "RadioButtonUI";
  98 
  99 
 100     /**
 101      * Creates an initially unselected radio button
 102      * with no set text.
 103      */
 104     public JRadioButton () {
 105         this(null, null, false);
 106     }
 107 
 108     /**
 109      * Creates an initially unselected radio button


 187     }
 188 
 189 
 190     /**
 191      * Resets the UI property to a value from the current look and feel.
 192      *
 193      * @see JComponent#updateUI
 194      */
 195     public void updateUI() {
 196         setUI((ButtonUI)UIManager.getUI(this));
 197     }
 198 
 199 
 200     /**
 201      * Returns the name of the L&amp;F class
 202      * that renders this component.
 203      *
 204      * @return String "RadioButtonUI"
 205      * @see JComponent#getUIClassID
 206      * @see UIDefaults#getUI
 207      * @beaninfo
 208      *        expert: true
 209      *   description: A string that specifies the name of the L&amp;F class.
 210      */


 211     public String getUIClassID() {
 212         return uiClassID;
 213     }
 214 
 215 
 216     /**
 217      * The icon for radio buttons comes from the look and feel,
 218      * not the Action.
 219      */
 220     void setIconFromAction(Action a) {
 221     }
 222 
 223     /**
 224      * See readObject() and writeObject() in JComponent for more
 225      * information about serialization in Swing.
 226      */
 227     private void writeObject(ObjectOutputStream s) throws IOException {
 228         s.defaultWriteObject();
 229         if (getUIClassID().equals(uiClassID)) {
 230             byte count = JComponent.getWriteObjCounter(this);


 246      * @return  a string representation of this JRadioButton.
 247      */
 248     protected String paramString() {
 249         return super.paramString();
 250     }
 251 
 252 
 253 /////////////////
 254 // Accessibility support
 255 ////////////////
 256 
 257 
 258     /**
 259      * Gets the AccessibleContext associated with this JRadioButton.
 260      * For JRadioButtons, the AccessibleContext takes the form of an
 261      * AccessibleJRadioButton.
 262      * A new AccessibleJRadioButton instance is created if necessary.
 263      *
 264      * @return an AccessibleJRadioButton that serves as the
 265      *         AccessibleContext of this JRadioButton
 266      * @beaninfo
 267      *       expert: true
 268      *  description: The AccessibleContext associated with this Button
 269      */


 270     public AccessibleContext getAccessibleContext() {
 271         if (accessibleContext == null) {
 272             accessibleContext = new AccessibleJRadioButton();
 273         }
 274         return accessibleContext;
 275     }
 276 
 277     /**
 278      * This class implements accessibility support for the
 279      * <code>JRadioButton</code> class.  It provides an implementation of the
 280      * Java Accessibility API appropriate to radio button
 281      * user-interface elements.
 282      * <p>
 283      * <strong>Warning:</strong>
 284      * Serialized objects of this class will not be compatible with
 285      * future Swing releases. The current serialization support is
 286      * appropriate for short term storage or RMI between applications running
 287      * the same version of Swing.  As of 1.4, support for long term storage
 288      * of all JavaBeans&trade;
 289      * has been added to the <code>java.beans</code> package.


   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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.beans.JavaBean;
  28 import java.beans.BeanProperty;

  29 
  30 import javax.swing.plaf.*;
  31 import javax.accessibility.*;
  32 
  33 import java.io.ObjectOutputStream;

  34 import java.io.IOException;
  35 

  36 /**
  37  * An implementation of a radio button -- an item that can be selected or
  38  * deselected, and which displays its state to the user.
  39  * Used with a {@link ButtonGroup} object to create a group of buttons
  40  * in which only one button at a time can be selected. (Create a ButtonGroup
  41  * object and use its <code>add</code> method to include the JRadioButton objects
  42  * in the group.)
  43  * <blockquote>
  44  * <strong>Note:</strong>
  45  * The ButtonGroup object is a logical grouping -- not a physical grouping.
  46  * To create a button panel, you should still create a {@link JPanel} or similar
  47  * container-object and add a {@link javax.swing.border.Border} to it to set it off from surrounding
  48  * components.
  49  * </blockquote>
  50  * <p>
  51  * Buttons can be configured, and to some degree controlled, by
  52  * <code><a href="Action.html">Action</a></code>s.  Using an
  53  * <code>Action</code> with a button has many benefits beyond directly
  54  * configuring a button.  Refer to <a href="Action.html#buttonActions">
  55  * Swing Components Supporting <code>Action</code></a> for more


  58  * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
  59  * <p>
  60  * See <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
  61  * in <em>The Java Tutorial</em>
  62  * for further documentation.
  63  * <p>
  64  * <strong>Warning:</strong> Swing is not thread safe. For more
  65  * information see <a
  66  * href="package-summary.html#threading">Swing's Threading
  67  * Policy</a>.
  68  * <p>
  69  * <strong>Warning:</strong>
  70  * Serialized objects of this class will not be compatible with
  71  * future Swing releases. The current serialization support is
  72  * appropriate for short term storage or RMI between applications running
  73  * the same version of Swing.  As of 1.4, support for long term storage
  74  * of all JavaBeans&trade;
  75  * has been added to the <code>java.beans</code> package.
  76  * Please see {@link java.beans.XMLEncoder}.
  77  *




  78  * @see ButtonGroup
  79  * @see JCheckBox
  80  * @author Jeff Dinkins
  81  * @since 1.2
  82  */
  83 @JavaBean(description = "A component which can display it's state as selected or deselected.")
  84 @SwingContainer(false)
  85 @SuppressWarnings("serial") // Same-version serialization only
  86 public class JRadioButton extends JToggleButton implements Accessible {
  87 
  88     /**
  89      * @see #getUIClassID
  90      * @see #readObject
  91      */
  92     private static final String uiClassID = "RadioButtonUI";
  93 
  94 
  95     /**
  96      * Creates an initially unselected radio button
  97      * with no set text.
  98      */
  99     public JRadioButton () {
 100         this(null, null, false);
 101     }
 102 
 103     /**
 104      * Creates an initially unselected radio button


 182     }
 183 
 184 
 185     /**
 186      * Resets the UI property to a value from the current look and feel.
 187      *
 188      * @see JComponent#updateUI
 189      */
 190     public void updateUI() {
 191         setUI((ButtonUI)UIManager.getUI(this));
 192     }
 193 
 194 
 195     /**
 196      * Returns the name of the L&amp;F class
 197      * that renders this component.
 198      *
 199      * @return String "RadioButtonUI"
 200      * @see JComponent#getUIClassID
 201      * @see UIDefaults#getUI



 202      */
 203     @BeanProperty(bound = false, expert = true, description
 204             = "A string that specifies the name of the L&amp;F class.")
 205     public String getUIClassID() {
 206         return uiClassID;
 207     }
 208 
 209 
 210     /**
 211      * The icon for radio buttons comes from the look and feel,
 212      * not the Action.
 213      */
 214     void setIconFromAction(Action a) {
 215     }
 216 
 217     /**
 218      * See readObject() and writeObject() in JComponent for more
 219      * information about serialization in Swing.
 220      */
 221     private void writeObject(ObjectOutputStream s) throws IOException {
 222         s.defaultWriteObject();
 223         if (getUIClassID().equals(uiClassID)) {
 224             byte count = JComponent.getWriteObjCounter(this);


 240      * @return  a string representation of this JRadioButton.
 241      */
 242     protected String paramString() {
 243         return super.paramString();
 244     }
 245 
 246 
 247 /////////////////
 248 // Accessibility support
 249 ////////////////
 250 
 251 
 252     /**
 253      * Gets the AccessibleContext associated with this JRadioButton.
 254      * For JRadioButtons, the AccessibleContext takes the form of an
 255      * AccessibleJRadioButton.
 256      * A new AccessibleJRadioButton instance is created if necessary.
 257      *
 258      * @return an AccessibleJRadioButton that serves as the
 259      *         AccessibleContext of this JRadioButton



 260      */
 261     @BeanProperty(bound = false, expert = true, description
 262             = "The AccessibleContext associated with this Button")
 263     public AccessibleContext getAccessibleContext() {
 264         if (accessibleContext == null) {
 265             accessibleContext = new AccessibleJRadioButton();
 266         }
 267         return accessibleContext;
 268     }
 269 
 270     /**
 271      * This class implements accessibility support for the
 272      * <code>JRadioButton</code> class.  It provides an implementation of the
 273      * Java Accessibility API appropriate to radio button
 274      * user-interface elements.
 275      * <p>
 276      * <strong>Warning:</strong>
 277      * Serialized objects of this class will not be compatible with
 278      * future Swing releases. The current serialization support is
 279      * appropriate for short term storage or RMI between applications running
 280      * the same version of Swing.  As of 1.4, support for long term storage
 281      * of all JavaBeans&trade;
 282      * has been added to the <code>java.beans</code> package.