001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing;
026
027 import java.awt.*;
028 import java.awt.event.*;
029 import java.beans.*;
030
031 import javax.swing.plaf.*;
032 import javax.accessibility.*;
033
034 import java.io.ObjectOutputStream;
035 import java.io.ObjectInputStream;
036 import java.io.IOException;
037
038 /**
039 * An implementation of a radio button -- an item that can be selected or
040 * deselected, and which displays its state to the user.
041 * Used with a {@link ButtonGroup} object to create a group of buttons
042 * in which only one button at a time can be selected. (Create a ButtonGroup
043 * object and use its <code>add</code> method to include the JRadioButton objects
044 * in the group.)
045 * <blockquote>
046 * <strong>Note:</strong>
047 * The ButtonGroup object is a logical grouping -- not a physical grouping.
048 * Tocreate a button panel, you should still create a {@link JPanel} or similar
049 * container-object and add a {@link javax.swing.border.Border} to it to set it off from surrounding
050 * components.
051 * </blockquote>
052 * <p>
053 * Buttons can be configured, and to some degree controlled, by
054 * <code><a href="Action.html">Action</a></code>s. Using an
055 * <code>Action</code> with a button has many benefits beyond directly
056 * configuring a button. Refer to <a href="Action.html#buttonActions">
057 * Swing Components Supporting <code>Action</code></a> for more
058 * details, and you can find more information in <a
059 * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
060 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
061 * <p>
062 * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
063 * in <em>The Java Tutorial</em>
064 * for further documentation.
065 * <p>
066 * <strong>Warning:</strong> Swing is not thread safe. For more
067 * information see <a
068 * href="package-summary.html#threading">Swing's Threading
069 * Policy</a>.
070 * <p>
071 * <strong>Warning:</strong>
072 * Serialized objects of this class will not be compatible with
073 * future Swing releases. The current serialization support is
074 * appropriate for short term storage or RMI between applications running
075 * the same version of Swing. As of 1.4, support for long term storage
076 * of all JavaBeans<sup><font size="-2">TM</font></sup>
077 * has been added to the <code>java.beans</code> package.
078 * Please see {@link java.beans.XMLEncoder}.
079 *
080 * @beaninfo
081 * attribute: isContainer false
082 * description: A component which can display it's state as selected or deselected.
083 *
084 * @see ButtonGroup
085 * @see JCheckBox
086 * @version 1.84 05/05/07
087 * @author Jeff Dinkins
088 */
089 public class JRadioButton extends JToggleButton implements Accessible {
090
091 /**
092 * @see #getUIClassID
093 * @see #readObject
094 */
095 private static final String uiClassID = "RadioButtonUI";
096
097 /**
098 * Creates an initially unselected radio button
099 * with no set text.
100 */
101 public JRadioButton() {
102 this (null, null, false);
103 }
104
105 /**
106 * Creates an initially unselected radio button
107 * with the specified image but no text.
108 *
109 * @param icon the image that the button should display
110 */
111 public JRadioButton(Icon icon) {
112 this (null, icon, false);
113 }
114
115 /**
116 * Creates a radiobutton where properties are taken from the
117 * Action supplied.
118 *
119 * @since 1.3
120 */
121 public JRadioButton(Action a) {
122 this ();
123 setAction(a);
124 }
125
126 /**
127 * Creates a radio button with the specified image
128 * and selection state, but no text.
129 *
130 * @param icon the image that the button should display
131 * @param selected if true, the button is initially selected;
132 * otherwise, the button is initially unselected
133 */
134 public JRadioButton(Icon icon, boolean selected) {
135 this (null, icon, selected);
136 }
137
138 /**
139 * Creates an unselected radio button with the specified text.
140 *
141 * @param text the string displayed on the radio button
142 */
143 public JRadioButton(String text) {
144 this (text, null, false);
145 }
146
147 /**
148 * Creates a radio button with the specified text
149 * and selection state.
150 *
151 * @param text the string displayed on the radio button
152 * @param selected if true, the button is initially selected;
153 * otherwise, the button is initially unselected
154 */
155 public JRadioButton(String text, boolean selected) {
156 this (text, null, selected);
157 }
158
159 /**
160 * Creates a radio button that has the specified text and image,
161 * and that is initially unselected.
162 *
163 * @param text the string displayed on the radio button
164 * @param icon the image that the button should display
165 */
166 public JRadioButton(String text, Icon icon) {
167 this (text, icon, false);
168 }
169
170 /**
171 * Creates a radio button that has the specified text, image,
172 * and selection state.
173 *
174 * @param text the string displayed on the radio button
175 * @param icon the image that the button should display
176 */
177 public JRadioButton(String text, Icon icon, boolean selected) {
178 super (text, icon, selected);
179 setBorderPainted(false);
180 setHorizontalAlignment(LEADING);
181 }
182
183 /**
184 * Resets the UI property to a value from the current look and feel.
185 *
186 * @see JComponent#updateUI
187 */
188 public void updateUI() {
189 setUI((ButtonUI) UIManager.getUI(this ));
190 }
191
192 /**
193 * Returns the name of the L&F class
194 * that renders this component.
195 *
196 * @return String "RadioButtonUI"
197 * @see JComponent#getUIClassID
198 * @see UIDefaults#getUI
199 * @beaninfo
200 * expert: true
201 * description: A string that specifies the name of the L&F class.
202 */
203 public String getUIClassID() {
204 return uiClassID;
205 }
206
207 /**
208 * The icon for radio buttons comes from the look and feel,
209 * not the Action.
210 */
211 void setIconFromAction(Action a) {
212 }
213
214 /**
215 * See readObject() and writeObject() in JComponent for more
216 * information about serialization in Swing.
217 */
218 private void writeObject(ObjectOutputStream s) throws IOException {
219 s.defaultWriteObject();
220 if (getUIClassID().equals(uiClassID)) {
221 byte count = JComponent.getWriteObjCounter(this );
222 JComponent.setWriteObjCounter(this , --count);
223 if (count == 0 && ui != null) {
224 ui.installUI(this );
225 }
226 }
227 }
228
229 /**
230 * Returns a string representation of this JRadioButton. This method
231 * is intended to be used only for debugging purposes, and the
232 * content and format of the returned string may vary between
233 * implementations. The returned string may be empty but may not
234 * be <code>null</code>.
235 *
236 * @return a string representation of this JRadioButton.
237 */
238 protected String paramString() {
239 return super .paramString();
240 }
241
242 /////////////////
243 // Accessibility support
244 ////////////////
245
246 /**
247 * Gets the AccessibleContext associated with this JRadioButton.
248 * For JRadioButtons, the AccessibleContext takes the form of an
249 * AccessibleJRadioButton.
250 * A new AccessibleJRadioButton instance is created if necessary.
251 *
252 * @return an AccessibleJRadioButton that serves as the
253 * AccessibleContext of this JRadioButton
254 * @beaninfo
255 * expert: true
256 * description: The AccessibleContext associated with this Button
257 */
258 public AccessibleContext getAccessibleContext() {
259 if (accessibleContext == null) {
260 accessibleContext = new AccessibleJRadioButton();
261 }
262 return accessibleContext;
263 }
264
265 /**
266 * This class implements accessibility support for the
267 * <code>JRadioButton</code> class. It provides an implementation of the
268 * Java Accessibility API appropriate to radio button
269 * user-interface elements.
270 * <p>
271 * <strong>Warning:</strong>
272 * Serialized objects of this class will not be compatible with
273 * future Swing releases. The current serialization support is
274 * appropriate for short term storage or RMI between applications running
275 * the same version of Swing. As of 1.4, support for long term storage
276 * of all JavaBeans<sup><font size="-2">TM</font></sup>
277 * has been added to the <code>java.beans</code> package.
278 * Please see {@link java.beans.XMLEncoder}.
279 */
280 protected class AccessibleJRadioButton extends
281 AccessibleJToggleButton {
282
283 /**
284 * Get the role of this object.
285 *
286 * @return an instance of AccessibleRole describing the role of the object
287 * @see AccessibleRole
288 */
289 public AccessibleRole getAccessibleRole() {
290 return AccessibleRole.RADIO_BUTTON;
291 }
292
293 } // inner class AccessibleJRadioButton
294 }
|