KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JRadioButton


1 /*
2  * @(#)JRadioButton.java 1.74 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.beans.*;
12
13 import javax.swing.plaf.*;
14 import javax.accessibility.*;
15
16 import java.io.ObjectOutputStream JavaDoc;
17 import java.io.ObjectInputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19
20
21 /**
22  * An implementation of a radio button -- an item that can be selected or
23  * deselected, and which displays its state to the user.
24  * Used with a {@link ButtonGroup} object to create a group of buttons
25  * in which only one button at a time can be selected. (Create a ButtonGroup
26  * object and use its <code>add</code> method to include the JRadioButton objects
27  * in the group.)
28  * <blockquote>
29  * <strong>Note:</strong>
30  * The ButtonGroup object is a logical grouping -- not a physical grouping.
31  * Tocreate a button panel, you should still create a {@link JPanel} or similar
32  * container-object and add a {@link javax.swing.border.Border} to it to set it off from surrounding
33  * components.
34  * </blockquote>
35  * <p>
36  * See <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
37  * in <em>The Java Tutorial</em>
38  * for further documentation.
39  * <p>
40  * <strong>Warning:</strong>
41  * Serialized objects of this class will not be compatible with
42  * future Swing releases. The current serialization support is
43  * appropriate for short term storage or RMI between applications running
44  * the same version of Swing. As of 1.4, support for long term storage
45  * of all JavaBeans<sup><font size="-2">TM</font></sup>
46  * has been added to the <code>java.beans</code> package.
47  * Please see {@link java.beans.XMLEncoder}.
48  *
49  * @beaninfo
50  * attribute: isContainer false
51  * description: A component which can display it's state as selected or deselected.
52  *
53  * @see ButtonGroup
54  * @see JCheckBox
55  * @version 1.74 12/19/03
56  * @author Jeff Dinkins
57  */

58 public class JRadioButton extends JToggleButton JavaDoc implements Accessible {
59
60     /**
61      * @see #getUIClassID
62      * @see #readObject
63      */

64     private static final String JavaDoc uiClassID = "RadioButtonUI";
65
66
67     /**
68      * Creates an initially unselected radio button
69      * with no set text.
70      */

71     public JRadioButton () {
72         this(null, null, false);
73     }
74      
75     /**
76      * Creates an initially unselected radio button
77      * with the specified image but no text.
78      *
79      * @param icon the image that the button should display
80      */

81     public JRadioButton(Icon JavaDoc icon) {
82         this(null, icon, false);
83     }
84
85     /**
86      * Creates a radiobutton where properties are taken from the
87      * Action supplied.
88      *
89      * @since 1.3
90      */

91     public JRadioButton(Action JavaDoc a) {
92         this();
93     setAction(a);
94     }
95
96     /**
97      * Creates a radio button with the specified image
98      * and selection state, but no text.
99      *
100      * @param icon the image that the button should display
101      * @param selected if true, the button is initially selected;
102      * otherwise, the button is initially unselected
103      */

104     public JRadioButton(Icon JavaDoc icon, boolean selected) {
105         this(null, icon, selected);
106     }
107     
108     /**
109      * Creates an unselected radio button with the specified text.
110      *
111      * @param text the string displayed on the radio button
112      */

113     public JRadioButton (String JavaDoc text) {
114         this(text, null, false);
115     }
116
117     /**
118      * Creates a radio button with the specified text
119      * and selection state.
120      *
121      * @param text the string displayed on the radio button
122      * @param selected if true, the button is initially selected;
123      * otherwise, the button is initially unselected
124      */

125     public JRadioButton (String JavaDoc text, boolean selected) {
126         this(text, null, selected);
127     }
128
129     /**
130      * Creates a radio button that has the specified text and image,
131      * and that is initially unselected.
132      *
133      * @param text the string displayed on the radio button
134      * @param icon the image that the button should display
135      */

136     public JRadioButton(String JavaDoc text, Icon JavaDoc icon) {
137         this(text, icon, false);
138     }
139
140     /**
141      * Creates a radio button that has the specified text, image,
142      * and selection state.
143      *
144      * @param text the string displayed on the radio button
145      * @param icon the image that the button should display
146      */

147     public JRadioButton (String JavaDoc text, Icon JavaDoc icon, boolean selected) {
148         super(text, icon, selected);
149         setBorderPainted(false);
150         setHorizontalAlignment(LEADING);
151     }
152
153
154     /**
155      * Resets the UI property to a value from the current look and feel.
156      *
157      * @see JComponent#updateUI
158      */

159     public void updateUI() {
160         setUI((ButtonUI)UIManager.getUI(this));
161     }
162     
163
164     /**
165      * Returns the name of the L&F class
166      * that renders this component.
167      *
168      * @return String "RadioButtonUI"
169      * @see JComponent#getUIClassID
170      * @see UIDefaults#getUI
171      * @beaninfo
172      * expert: true
173      * description: A string that specifies the name of the L&F class.
174      */

175     public String JavaDoc getUIClassID() {
176         return uiClassID;
177     }
178
179
180     /**
181      * Factory method which sets the <code>ActionEvent</code> source's
182      * properties according to values from the Action instance. The
183      * properties which are set may differ for subclasses.
184      * By default, the properties which get set are <code>Text, Mnemonic,
185      * Enabled, ActionCommand</code>, and <code>ToolTipText</code>.
186      *
187      * @param a the Action from which to get the properties, or null
188      * @since 1.3
189      * @see Action
190      * @see #setAction
191      */

192     protected void configurePropertiesFromAction(Action JavaDoc a) {
193         String JavaDoc[] types = { Action.MNEMONIC_KEY, Action.NAME,
194                            Action.SHORT_DESCRIPTION,
195                            Action.ACTION_COMMAND_KEY, "enabled" };
196         configurePropertiesFromAction(a, types);
197     }
198
199     /**
200      * Factory method which creates the PropertyChangeListener
201      * used to update the ActionEvent source as properties change on
202      * its Action instance. Subclasses may override this in order
203      * to provide their own PropertyChangeListener if the set of
204      * properties which should be kept up to date differs from the
205      * default properties (Text, Icon, Enabled, ToolTipText).
206      *
207      * Note that PropertyChangeListeners should avoid holding
208      * strong references to the ActionEvent source, as this may hinder
209      * garbage collection of the ActionEvent source and all components
210      * in its containment hierarchy.
211      *
212      * @since 1.3
213      * @see Action
214      * @see #setAction
215      */

216     protected PropertyChangeListener createActionPropertyChangeListener(Action JavaDoc a) {
217         return new AbstractActionPropertyChangeListener JavaDoc(this, a) {
218         public void propertyChange(PropertyChangeEvent e) {
219         String JavaDoc propertyName = e.getPropertyName();
220         AbstractButton JavaDoc button = (AbstractButton JavaDoc)getTarget();
221         if (button == null) { //WeakRef GC'ed in 1.2
222
Action JavaDoc action = (Action JavaDoc)e.getSource();
223             action.removePropertyChangeListener(this);
224         } else {
225             if (propertyName.equals(Action.NAME)) {
226             String JavaDoc text = (String JavaDoc) e.getNewValue();
227             button.setText(text);
228             button.repaint();
229             } else if (propertyName.equals(Action.SHORT_DESCRIPTION)) {
230             String JavaDoc text = (String JavaDoc) e.getNewValue();
231             button.setToolTipText(text);
232             } else if (propertyName.equals("enabled")) {
233             Boolean JavaDoc enabledState = (Boolean JavaDoc) e.getNewValue();
234             button.setEnabled(enabledState.booleanValue());
235             button.repaint();
236                     } else if (propertyName.equals(Action.ACTION_COMMAND_KEY)) {
237                         button.setActionCommand((String JavaDoc)e.getNewValue());
238             }
239         }
240         }
241     };
242     }
243
244     /**
245      * See readObject() and writeObject() in JComponent for more
246      * information about serialization in Swing.
247      */

248     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
249         s.defaultWriteObject();
250         if (getUIClassID().equals(uiClassID)) {
251             byte count = JComponent.getWriteObjCounter(this);
252             JComponent.setWriteObjCounter(this, --count);
253             if (count == 0 && ui != null) {
254                 ui.installUI(this);
255             }
256         }
257     }
258
259
260     /**
261      * Returns a string representation of this JRadioButton. This method
262      * is intended to be used only for debugging purposes, and the
263      * content and format of the returned string may vary between
264      * implementations. The returned string may be empty but may not
265      * be <code>null</code>.
266      *
267      * @return a string representation of this JRadioButton.
268      */

269     protected String JavaDoc paramString() {
270     return super.paramString();
271     }
272
273
274 /////////////////
275
// Accessibility support
276
////////////////
277

278
279     /**
280      * Gets the AccessibleContext associated with this JRadioButton.
281      * For JRadioButtons, the AccessibleContext takes the form of an
282      * AccessibleJRadioButton.
283      * A new AccessibleJRadioButton instance is created if necessary.
284      *
285      * @return an AccessibleJRadioButton that serves as the
286      * AccessibleContext of this JRadioButton
287      * @beaninfo
288      * expert: true
289      * description: The AccessibleContext associated with this Button
290      */

291     public AccessibleContext getAccessibleContext() {
292         if (accessibleContext == null) {
293             accessibleContext = new AccessibleJRadioButton();
294         }
295         return accessibleContext;
296     }
297
298     /**
299      * This class implements accessibility support for the
300      * <code>JRadioButton</code> class. It provides an implementation of the
301      * Java Accessibility API appropriate to radio button
302      * user-interface elements.
303      * <p>
304      * <strong>Warning:</strong>
305      * Serialized objects of this class will not be compatible with
306      * future Swing releases. The current serialization support is
307      * appropriate for short term storage or RMI between applications running
308      * the same version of Swing. As of 1.4, support for long term storage
309      * of all JavaBeans<sup><font size="-2">TM</font></sup>
310      * has been added to the <code>java.beans</code> package.
311      * Please see {@link java.beans.XMLEncoder}.
312      */

313     protected class AccessibleJRadioButton extends AccessibleJToggleButton {
314
315         /**
316          * Get the role of this object.
317          *
318          * @return an instance of AccessibleRole describing the role of the object
319          * @see AccessibleRole
320          */

321         public AccessibleRole getAccessibleRole() {
322             return AccessibleRole.RADIO_BUTTON;
323         }
324
325     } // inner class AccessibleJRadioButton
326
}
327   
328
Popular Tags