KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JRadioButtonMenuItem


1 /*
2  * @(#)JRadioButtonMenuItem.java 1.49 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.util.EventListener JavaDoc;
10
11 import java.awt.*;
12 import java.awt.event.*;
13 import java.awt.image.*;
14
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18
19 import javax.swing.plaf.*;
20 import javax.accessibility.*;
21
22 /**
23  * An implementation of a radio button menu item.
24  * A <code>JRadioButtonMenuItem</code> is
25  * a menu item that is part of a group of menu items in which only one
26  * item in the group can be selected. The selected item displays its
27  * selected state. Selecting it causes any other selected item to
28  * switch to the unselected state.
29  * To control the selected state of a group of radio button menu items,
30  * use a <code>ButtonGroup</code> object.
31  * <p>
32  * For further documentation and examples see
33  * <a
34  href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
35  * a section in <em>The Java Tutorial.</em>
36  * <p>
37  * <strong>Warning:</strong>
38  * Serialized objects of this class will not be compatible with
39  * future Swing releases. The current serialization support is
40  * appropriate for short term storage or RMI between applications running
41  * the same version of Swing. As of 1.4, support for long term storage
42  * of all JavaBeans<sup><font size="-2">TM</font></sup>
43  * has been added to the <code>java.beans</code> package.
44  * Please see {@link java.beans.XMLEncoder}.
45  *
46  * @beaninfo
47  * attribute: isContainer false
48  * description: A component within a group of menu items which can be selected.
49  *
50  * @version 1.49 12/19/03
51  * @author Georges Saab
52  * @author David Karlton
53  * @see ButtonGroup
54  */

55 public class JRadioButtonMenuItem extends JMenuItem JavaDoc implements Accessible {
56     /**
57      * @see #getUIClassID
58      * @see #readObject
59      */

60     private static final String JavaDoc uiClassID = "RadioButtonMenuItemUI";
61
62     /**
63      * Creates a <code>JRadioButtonMenuItem</code> with no set text or icon.
64      */

65     public JRadioButtonMenuItem() {
66         this(null, null, false);
67     }
68
69     /**
70      * Creates a <code>JRadioButtonMenuItem</code> with an icon.
71      *
72      * @param icon the <code>Icon</code> to display on the
73      * <code>JRadioButtonMenuItem</code>
74      */

75     public JRadioButtonMenuItem(Icon JavaDoc icon) {
76         this(null, icon, false);
77     }
78
79     /**
80      * Creates a <code>JRadioButtonMenuItem</code> with text.
81      *
82      * @param text the text of the <code>JRadioButtonMenuItem</code>
83      */

84     public JRadioButtonMenuItem(String JavaDoc text) {
85         this(text, null, false);
86     }
87     
88     /**
89      * Creates a radio button menu item whose properties are taken from the
90      * <code>Action</code> supplied.
91      *
92      * @param a the <code>Action</code> on which to base the radio
93      * button menu item
94      *
95      * @since 1.3
96      */

97     public JRadioButtonMenuItem(Action JavaDoc a) {
98         this();
99     setAction(a);
100     }
101
102     /**
103      * Creates a radio button menu item with the specified text
104      * and <code>Icon</code>.
105      *
106      * @param text the text of the <code>JRadioButtonMenuItem</code>
107      * @param icon the icon to display on the <code>JRadioButtonMenuItem</code>
108      */

109     public JRadioButtonMenuItem(String JavaDoc text, Icon JavaDoc icon) {
110     this(text, icon, false);
111     }
112
113     /**
114      * Creates a radio button menu item with the specified text
115      * and selection state.
116      *
117      * @param text the text of the <code>CheckBoxMenuItem</code>
118      * @param selected the selected state of the <code>CheckBoxMenuItem</code>
119      */

120     public JRadioButtonMenuItem(String JavaDoc text, boolean selected) {
121         this(text);
122         setSelected(selected);
123     }
124
125     /**
126      * Creates a radio button menu item 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 JRadioButtonMenuItem(Icon JavaDoc icon, boolean selected) {
134         this(null, icon, selected);
135     }
136
137     /**
138      * Creates a radio button menu item that has the specified
139      * text, image, and selection state. All other constructors
140      * defer to this one.
141      *
142      * @param text the string displayed on the radio button
143      * @param icon the image that the button should display
144      */

145     public JRadioButtonMenuItem(String JavaDoc text, Icon JavaDoc icon, boolean selected) {
146     super(text, icon);
147         setModel(new JToggleButton.ToggleButtonModel JavaDoc());
148         setSelected(selected);
149     setFocusable(false);
150     }
151
152     /**
153      * Returns the name of the L&F class that renders this component.
154      *
155      * @return the string "RadioButtonMenuItemUI"
156      * @see JComponent#getUIClassID
157      * @see UIDefaults#getUI
158      */

159     public String JavaDoc getUIClassID() {
160         return uiClassID;
161     }
162
163     /**
164      * See <code>readObject</code> and <code>writeObject</code> in
165      * <code>JComponent</code> for more
166      * information about serialization in Swing.
167      */

168     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
169         s.defaultWriteObject();
170         if (getUIClassID().equals(uiClassID)) {
171             byte count = JComponent.getWriteObjCounter(this);
172             JComponent.setWriteObjCounter(this, --count);
173             if (count == 0 && ui != null) {
174                 ui.installUI(this);
175             }
176         }
177     }
178
179
180     /**
181      * Returns a string representation of this
182      * <code>JRadioButtonMenuItem</code>. This method
183      * is intended to be used only for debugging purposes, and the
184      * content and format of the returned string may vary between
185      * implementations. The returned string may be empty but may not
186      * be <code>null</code>.
187      *
188      * @return a string representation of this
189      * <code>JRadioButtonMenuItem</code>
190      */

191     protected String JavaDoc paramString() {
192     return super.paramString();
193     }
194
195 /////////////////
196
// Accessibility support
197
////////////////
198

199     /**
200      * Gets the AccessibleContext associated with this JRadioButtonMenuItem.
201      * For JRadioButtonMenuItems, the AccessibleContext takes the form of an
202      * AccessibleJRadioButtonMenuItem.
203      * A new AccessibleJRadioButtonMenuItem instance is created if necessary.
204      *
205      * @return an AccessibleJRadioButtonMenuItem that serves as the
206      * AccessibleContext of this JRadioButtonMenuItem
207      */

208     public AccessibleContext getAccessibleContext() {
209         if (accessibleContext == null) {
210             accessibleContext = new AccessibleJRadioButtonMenuItem();
211         }
212         return accessibleContext;
213     }
214
215     /**
216      * This class implements accessibility support for the
217      * <code>JRadioButtonMenuItem</code> class. It provides an
218      * implementation of the Java Accessibility API appropriate to
219      * <code>JRadioButtonMenuItem</code> user-interface elements.
220      * <p>
221      * <strong>Warning:</strong>
222      * Serialized objects of this class will not be compatible with
223      * future Swing releases. The current serialization support is
224      * appropriate for short term storage or RMI between applications running
225      * the same version of Swing. As of 1.4, support for long term storage
226      * of all JavaBeans<sup><font size="-2">TM</font></sup>
227      * has been added to the <code>java.beans</code> package.
228      * Please see {@link java.beans.XMLEncoder}.
229      */

230     protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem {
231         /**
232          * Get the role of this object.
233          *
234          * @return an instance of AccessibleRole describing the role of the
235          * object
236          */

237         public AccessibleRole getAccessibleRole() {
238             return AccessibleRole.RADIO_BUTTON;
239         }
240     } // inner class AccessibleJRadioButtonMenuItem
241
}
242
243
Popular Tags