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.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 56 * details, and you can find more information in <a 57 * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How 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™ 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 105 * with the specified image but no text. 106 * 107 * @param icon the image that the button should display 108 */ 109 public JRadioButton(Icon icon) { 110 this(null, icon, false); 111 } 112 113 /** 114 * Creates a radiobutton where properties are taken from the 115 * Action supplied. 116 * 117 * @param a an {@code Action} 118 * @since 1.3 119 */ 120 public JRadioButton(Action a) { 121 this(); 122 setAction(a); 123 } 124 125 /** 126 * Creates a radio button with the specified image 127 * and selection state, but no text. 128 * 129 * @param icon the image that the button should display 130 * @param selected if true, the button is initially selected; 131 * otherwise, the button is initially unselected 132 */ 133 public JRadioButton(Icon icon, boolean selected) { 134 this(null, icon, selected); 135 } 136 137 /** 138 * Creates an unselected radio button with the specified text. 139 * 140 * @param text the string displayed on the radio button 141 */ 142 public JRadioButton (String text) { 143 this(text, null, false); 144 } 145 146 /** 147 * Creates a radio button with the specified text 148 * and selection state. 149 * 150 * @param text the string displayed on the radio button 151 * @param selected if true, the button is initially selected; 152 * otherwise, the button is initially unselected 153 */ 154 public JRadioButton (String text, boolean selected) { 155 this(text, null, selected); 156 } 157 158 /** 159 * Creates a radio button that has the specified text and image, 160 * and that is initially unselected. 161 * 162 * @param text the string displayed on the radio button 163 * @param icon the image that the button should display 164 */ 165 public JRadioButton(String text, Icon icon) { 166 this(text, icon, false); 167 } 168 169 /** 170 * Creates a radio button that has the specified text, image, 171 * and selection state. 172 * 173 * @param text the string displayed on the radio button 174 * @param icon the image that the button should display 175 * @param selected if {@code true}, the button is initially selected 176 * otherwise, the button is initially unselected 177 */ 178 public JRadioButton (String text, Icon icon, boolean selected) { 179 super(text, icon, selected); 180 setBorderPainted(false); 181 setHorizontalAlignment(LEADING); 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&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&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); 225 JComponent.setWriteObjCounter(this, --count); 226 if (count == 0 && ui != null) { 227 ui.installUI(this); 228 } 229 } 230 } 231 232 233 /** 234 * Returns a string representation of this JRadioButton. This method 235 * is intended to be used only for debugging purposes, and the 236 * content and format of the returned string may vary between 237 * implementations. The returned string may be empty but may not 238 * be <code>null</code>. 239 * 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™ 282 * has been added to the <code>java.beans</code> package. 283 * Please see {@link java.beans.XMLEncoder}. 284 */ 285 @SuppressWarnings("serial") // Same-version serialization only 286 protected class AccessibleJRadioButton extends AccessibleJToggleButton { 287 288 /** 289 * Get the role of this object. 290 * 291 * @return an instance of AccessibleRole describing the role of the object 292 * @see AccessibleRole 293 */ 294 public AccessibleRole getAccessibleRole() { 295 return AccessibleRole.RADIO_BUTTON; 296 } 297 298 } // inner class AccessibleJRadioButton 299 }