1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 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 59 * details, and you can find more information in <a 60 * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How 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™ 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 110 * with the specified image but no text. 111 * 112 * @param icon the image that the button should display 113 */ 114 public JRadioButton(Icon icon) { 115 this(null, icon, false); 116 } 117 118 /** 119 * Creates a radiobutton where properties are taken from the 120 * Action supplied. 121 * 122 * @since 1.3 123 */ 124 public JRadioButton(Action a) { 125 this(); 126 setAction(a); 127 } 128 129 /** 130 * Creates a radio button with the specified image 131 * and selection state, but no text. 132 * 133 * @param icon the image that the button should display 134 * @param selected if true, the button is initially selected; 135 * otherwise, the button is initially unselected 136 */ 137 public JRadioButton(Icon icon, boolean selected) { 138 this(null, icon, selected); 139 } 140 141 /** 142 * Creates an unselected radio button with the specified text. 143 * 144 * @param text the string displayed on the radio button 145 */ 146 public JRadioButton (String text) { 147 this(text, null, false); 148 } 149 150 /** 151 * Creates a radio button with the specified text 152 * and selection state. 153 * 154 * @param text the string displayed on the radio button 155 * @param selected if true, the button is initially selected; 156 * otherwise, the button is initially unselected 157 */ 158 public JRadioButton (String text, boolean selected) { 159 this(text, null, selected); 160 } 161 162 /** 163 * Creates a radio button that has the specified text and image, 164 * and that is initially unselected. 165 * 166 * @param text the string displayed on the radio button 167 * @param icon the image that the button should display 168 */ 169 public JRadioButton(String text, Icon icon) { 170 this(text, icon, false); 171 } 172 173 /** 174 * Creates a radio button that has the specified text, image, 175 * and selection state. 176 * 177 * @param text the string displayed on the radio button 178 * @param icon the image that the button should display 179 */ 180 public JRadioButton (String text, Icon icon, boolean selected) { 181 super(text, icon, selected); 182 setBorderPainted(false); 183 setHorizontalAlignment(LEADING); 184 } 185 186 187 /** 188 * Resets the UI property to a value from the current look and feel. 189 * 190 * @see JComponent#updateUI 191 */ 192 public void updateUI() { 193 setUI((ButtonUI)UIManager.getUI(this)); 194 } 195 196 197 /** 198 * Returns the name of the L&F class 199 * that renders this component. 200 * 201 * @return String "RadioButtonUI" 202 * @see JComponent#getUIClassID 203 * @see UIDefaults#getUI 204 * @beaninfo 205 * expert: true 206 * description: A string that specifies the name of the L&F class. 207 */ 208 public String getUIClassID() { 209 return uiClassID; 210 } 211 212 213 /** 214 * The icon for radio buttons comes from the look and feel, 215 * not the Action. 216 */ 217 void setIconFromAction(Action a) { 218 } 219 220 /** 221 * See readObject() and writeObject() in JComponent for more 222 * information about serialization in Swing. 223 */ 224 private void writeObject(ObjectOutputStream s) throws IOException { 225 s.defaultWriteObject(); 226 if (getUIClassID().equals(uiClassID)) { 227 byte count = JComponent.getWriteObjCounter(this); 228 JComponent.setWriteObjCounter(this, --count); 229 if (count == 0 && ui != null) { 230 ui.installUI(this); 231 } 232 } 233 } 234 235 236 /** 237 * Returns a string representation of this JRadioButton. This method 238 * is intended to be used only for debugging purposes, and the 239 * content and format of the returned string may vary between 240 * implementations. The returned string may be empty but may not 241 * be <code>null</code>. 242 * 243 * @return a string representation of this JRadioButton. 244 */ 245 protected String paramString() { 246 return super.paramString(); 247 } 248 249 250 ///////////////// 251 // Accessibility support 252 //////////////// 253 254 255 /** 256 * Gets the AccessibleContext associated with this JRadioButton. 257 * For JRadioButtons, the AccessibleContext takes the form of an 258 * AccessibleJRadioButton. 259 * A new AccessibleJRadioButton instance is created if necessary. 260 * 261 * @return an AccessibleJRadioButton that serves as the 262 * AccessibleContext of this JRadioButton 263 * @beaninfo 264 * expert: true 265 * description: The AccessibleContext associated with this Button 266 */ 267 public AccessibleContext getAccessibleContext() { 268 if (accessibleContext == null) { 269 accessibleContext = new AccessibleJRadioButton(); 270 } 271 return accessibleContext; 272 } 273 274 /** 275 * This class implements accessibility support for the 276 * <code>JRadioButton</code> class. It provides an implementation of the 277 * Java Accessibility API appropriate to radio button 278 * user-interface elements. 279 * <p> 280 * <strong>Warning:</strong> 281 * Serialized objects of this class will not be compatible with 282 * future Swing releases. The current serialization support is 283 * appropriate for short term storage or RMI between applications running 284 * the same version of Swing. As of 1.4, support for long term storage 285 * of all JavaBeans™ 286 * has been added to the <code>java.beans</code> package. 287 * Please see {@link java.beans.XMLEncoder}. 288 */ 289 @SuppressWarnings("serial") // Same-version serialization only 290 protected class AccessibleJRadioButton extends AccessibleJToggleButton { 291 292 /** 293 * Get the role of this object. 294 * 295 * @return an instance of AccessibleRole describing the role of the object 296 * @see AccessibleRole 297 */ 298 public AccessibleRole getAccessibleRole() { 299 return AccessibleRole.RADIO_BUTTON; 300 } 301 302 } // inner class AccessibleJRadioButton 303 }