1. /*
  2. * @(#)JRadioButton.java 1.53 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.plaf.*;
  11. import javax.accessibility.*;
  12. import java.io.ObjectOutputStream;
  13. import java.io.ObjectInputStream;
  14. import java.io.IOException;
  15. /**
  16. * An implementation of a radio button -- an item that can be selected or
  17. * deselected, and which displays its state to the user.
  18. * Used with a {@link ButtonGroup} object to create a group of buttons
  19. * in which only one button at a time can be selected. (Create a ButtonGroup
  20. * object and use its <code>add</code> method to include the JRadioButton objects
  21. * in the group.)
  22. * <blockquote>
  23. * Note:<br>
  24. * The ButtonGroup object is a logical grouping -- not a physical grouping.
  25. * Tocreate a button panel, you should still create a {@link JPanel} or similar
  26. * container-object and add a {@link Border} to it to set it off from surrounding
  27. * components.
  28. * <blockquote>
  29. * <p>
  30. * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/radiobutton.html">How to Use Radio Buttons</a>
  31. * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  32. * for further documentation.
  33. * <p>
  34. * For the keyboard keys used by this component in the standard Look and
  35. * Feel (L&F) renditions, see the
  36. * <a href="doc-files/Key-Index.html#JRadioButton">JRadioButton</a> key assignments.
  37. * <p>
  38. * <strong>Warning:</strong>
  39. * Serialized objects of this class will not be compatible with
  40. * future Swing releases. The current serialization support is appropriate
  41. * for short term storage or RMI between applications running the same
  42. * version of Swing. A future release of Swing will provide support for
  43. * long term persistence.
  44. *
  45. * @beaninfo
  46. * attribute: isContainer false
  47. * @see ButtonGroup
  48. * @see JCheckBox
  49. * @version 1.53 11/29/01
  50. * @author Jeff Dinkins
  51. */
  52. public class JRadioButton extends JToggleButton implements Accessible {
  53. /**
  54. * @see #getUIClassID
  55. * @see #readObject
  56. */
  57. private static final String uiClassID = "RadioButtonUI";
  58. /**
  59. * Creates an initially unselected radio button
  60. * with no set text.
  61. */
  62. public JRadioButton () {
  63. this(null, null, false);
  64. }
  65. /**
  66. * Creates an initially unselected radio button
  67. * with the specified image but no text.
  68. *
  69. * @param icon the image that the button should display
  70. */
  71. public JRadioButton(Icon icon) {
  72. this(null, icon, false);
  73. }
  74. /**
  75. * Creates a radio button with the specified image
  76. * and selection state, but no text.
  77. *
  78. * @param icon the image that the button should display
  79. * @param selected if true, the button is initially selected;
  80. * otherwise, the button is initially unselected
  81. */
  82. public JRadioButton(Icon icon, boolean selected) {
  83. this(null, icon, selected);
  84. }
  85. /**
  86. * Creates an unselected radio button with the specified text.
  87. *
  88. * @param text the string displayed on the radio button
  89. */
  90. public JRadioButton (String text) {
  91. this(text, null, false);
  92. }
  93. /**
  94. * Creates a radio button with the specified text
  95. * and selection state.
  96. *
  97. * @param text the string displayed on the radio button
  98. * @param selected if true, the button is initially selected;
  99. * otherwise, the button is initially unselected
  100. */
  101. public JRadioButton (String text, boolean selected) {
  102. this(text, null, selected);
  103. }
  104. /**
  105. * Creates a radio button that has the specified text and image,
  106. * and that is initially unselected.
  107. *
  108. * @param text the string displayed on the radio button
  109. * @param icon the image that the button should display
  110. */
  111. public JRadioButton(String text, Icon icon) {
  112. this(text, icon, false);
  113. }
  114. /**
  115. * Creates a radio button that has the specified text, image,
  116. * and selection state.
  117. *
  118. * @param text the string displayed on the radio button
  119. * @param icon the image that the button should display
  120. */
  121. public JRadioButton (String text, Icon icon, boolean selected) {
  122. super(text, icon, selected);
  123. setBorderPainted(false);
  124. setHorizontalAlignment(LEFT);
  125. }
  126. /**
  127. * Notification from the UIFactory that the L&F
  128. * has changed.
  129. *
  130. * @see JComponent#updateUI
  131. */
  132. public void updateUI() {
  133. setUI((ButtonUI)UIManager.getUI(this));
  134. }
  135. /**
  136. * Returns the name of the L&F class
  137. * that renders this component.
  138. *
  139. * @return String "RadioButtonUI"
  140. * @see JComponent#getUIClassID
  141. * @see UIDefaults#getUI
  142. * @beaninfo
  143. * expert: true
  144. * description: A string that specifies the name of the L&F class.
  145. */
  146. public String getUIClassID() {
  147. return uiClassID;
  148. }
  149. /**
  150. * See readObject() and writeObject() in JComponent for more
  151. * information about serialization in Swing.
  152. */
  153. private void writeObject(ObjectOutputStream s) throws IOException {
  154. s.defaultWriteObject();
  155. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  156. ui.installUI(this);
  157. }
  158. }
  159. /**
  160. * Returns a string representation of this JRadioButton. This method
  161. * is intended to be used only for debugging purposes, and the
  162. * content and format of the returned string may vary between
  163. * implementations. The returned string may be empty but may not
  164. * be <code>null</code>.
  165. *
  166. * @return a string representation of this JRadioButton.
  167. */
  168. protected String paramString() {
  169. return super.paramString();
  170. }
  171. /////////////////
  172. // Accessibility support
  173. ////////////////
  174. /**
  175. * Get the AccessibleContext associated with this JComponent
  176. *
  177. * @return the AccessibleContext of this JComponent
  178. * @beaninfo
  179. * expert: true
  180. * description: The AccessibleContext associated with this Button
  181. */
  182. public AccessibleContext getAccessibleContext() {
  183. if (accessibleContext == null) {
  184. accessibleContext = new AccessibleJRadioButton();
  185. }
  186. return accessibleContext;
  187. }
  188. /**
  189. * The class used to obtain the accessible role for this object.
  190. * <p>
  191. * <strong>Warning:</strong>
  192. * Serialized objects of this class will not be compatible with
  193. * future Swing releases. The current serialization support is appropriate
  194. * for short term storage or RMI between applications running the same
  195. * version of Swing. A future release of Swing will provide support for
  196. * long term persistence.
  197. */
  198. protected class AccessibleJRadioButton extends AccessibleJToggleButton {
  199. /**
  200. * Get the role of this object.
  201. *
  202. * @return an instance of AccessibleRole describing the role of the object
  203. * @see AccessibleRole
  204. */
  205. public AccessibleRole getAccessibleRole() {
  206. return AccessibleRole.RADIO_BUTTON;
  207. }
  208. } // inner class AccessibleJRadioButton
  209. }