1. /*
  2. * @(#)JRadioButton.java 1.64 00/04/06
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import java.beans.*;
  14. import javax.swing.plaf.*;
  15. import javax.accessibility.*;
  16. import java.io.ObjectOutputStream;
  17. import java.io.ObjectInputStream;
  18. import java.io.IOException;
  19. /**
  20. * An implementation of a radio button -- an item that can be selected or
  21. * deselected, and which displays its state to the user.
  22. * Used with a {@link ButtonGroup} object to create a group of buttons
  23. * in which only one button at a time can be selected. (Create a ButtonGroup
  24. * object and use its <code>add</code> method to include the JRadioButton objects
  25. * in the group.)
  26. * <blockquote>
  27. * <strong>Note:</strong>
  28. * The ButtonGroup object is a logical grouping -- not a physical grouping.
  29. * Tocreate a button panel, you should still create a {@link JPanel} or similar
  30. * container-object and add a {@link javax.swing.border.Border} to it to set it off from surrounding
  31. * components.
  32. * </blockquote>
  33. * <p>
  34. * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
  35. * in <em>The Java Tutorial</em>
  36. * for further documentation.
  37. * <p>
  38. * For the keyboard keys used by this component in the standard Look and
  39. * Feel (L&F) renditions, see the
  40. * <a href="doc-files/Key-Index.html#JRadioButton">JRadioButton</a> key assignments.
  41. * <p>
  42. * <strong>Warning:</strong>
  43. * Serialized objects of this class will not be compatible with
  44. * future Swing releases. The current serialization support is appropriate
  45. * for short term storage or RMI between applications running the same
  46. * version of Swing. A future release of Swing will provide support for
  47. * long term persistence.
  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.64 04/06/00
  56. * @author Jeff Dinkins
  57. */
  58. public class JRadioButton extends JToggleButton implements Accessible {
  59. /**
  60. * @see #getUIClassID
  61. * @see #readObject
  62. */
  63. private static final String uiClassID = "RadioButtonUI";
  64. /**
  65. * Creates an initially unselected radio button
  66. * with no set text.
  67. */
  68. public JRadioButton () {
  69. this(null, null, false);
  70. }
  71. /**
  72. * Creates an initially unselected radio button
  73. * with the specified image but no text.
  74. *
  75. * @param icon the image that the button should display
  76. */
  77. public JRadioButton(Icon icon) {
  78. this(null, icon, false);
  79. }
  80. /**
  81. * Creates a radiobutton where properties are taken from the
  82. * Action supplied.
  83. *
  84. * @since 1.3
  85. */
  86. public JRadioButton(Action a) {
  87. this();
  88. setAction(a);
  89. }
  90. /**
  91. * Creates a radio button with the specified image
  92. * and selection state, but no text.
  93. *
  94. * @param icon the image that the button should display
  95. * @param selected if true, the button is initially selected;
  96. * otherwise, the button is initially unselected
  97. */
  98. public JRadioButton(Icon icon, boolean selected) {
  99. this(null, icon, selected);
  100. }
  101. /**
  102. * Creates an unselected radio button with the specified text.
  103. *
  104. * @param text the string displayed on the radio button
  105. */
  106. public JRadioButton (String text) {
  107. this(text, null, false);
  108. }
  109. /**
  110. * Creates a radio button with the specified text
  111. * and selection state.
  112. *
  113. * @param text the string displayed on the radio button
  114. * @param selected if true, the button is initially selected;
  115. * otherwise, the button is initially unselected
  116. */
  117. public JRadioButton (String text, boolean selected) {
  118. this(text, null, selected);
  119. }
  120. /**
  121. * Creates a radio button that has the specified text and image,
  122. * and that is initially unselected.
  123. *
  124. * @param text the string displayed on the radio button
  125. * @param icon the image that the button should display
  126. */
  127. public JRadioButton(String text, Icon icon) {
  128. this(text, icon, false);
  129. }
  130. /**
  131. * Creates a radio button that has the specified text, image,
  132. * and selection state.
  133. *
  134. * @param text the string displayed on the radio button
  135. * @param icon the image that the button should display
  136. */
  137. public JRadioButton (String text, Icon icon, boolean selected) {
  138. super(text, icon, selected);
  139. setBorderPainted(false);
  140. setHorizontalAlignment(LEFT);
  141. }
  142. /**
  143. * Notification from the UIFactory that the L&F
  144. * has changed.
  145. *
  146. * @see JComponent#updateUI
  147. */
  148. public void updateUI() {
  149. setUI((ButtonUI)UIManager.getUI(this));
  150. }
  151. /**
  152. * Returns the name of the L&F class
  153. * that renders this component.
  154. *
  155. * @return String "RadioButtonUI"
  156. * @see JComponent#getUIClassID
  157. * @see UIDefaults#getUI
  158. * @beaninfo
  159. * expert: true
  160. * description: A string that specifies the name of the L&F class.
  161. */
  162. public String getUIClassID() {
  163. return uiClassID;
  164. }
  165. /**
  166. * Factory method which sets the ActionEvent source's properties
  167. * according to values from the Action instance. The properties
  168. * which are set may differ for subclasses.
  169. * By default, the properties which get set are Text, Icon
  170. * Enabled, and ToolTipText.
  171. *
  172. * @param a the Action from which to get the properties, or null
  173. * @since 1.3
  174. * @see Action
  175. * @see #setAction
  176. */
  177. protected void configurePropertiesFromAction(Action a) {
  178. setText((a!=null?(String)a.getValue(Action.NAME):null));
  179. setEnabled((a!=null?a.isEnabled():true));
  180. setToolTipText((a!=null?(String)a.getValue(Action.SHORT_DESCRIPTION):null));
  181. }
  182. /**
  183. * Factory method which creates the PropertyChangeListener
  184. * used to update the ActionEvent source as properties change on
  185. * its Action instance. Subclasses may override this in order
  186. * to provide their own PropertyChangeListener if the set of
  187. * properties which should be kept up to date differs from the
  188. * default properties (Text, Icon, Enabled, ToolTipText).
  189. *
  190. * Note that PropertyChangeListeners should avoid holding
  191. * strong references to the ActionEvent source, as this may hinder
  192. * garbage collection of the ActionEvent source and all components
  193. * in its containment hierarchy.
  194. *
  195. * @since 1.3
  196. * @see Action
  197. * @see #setAction
  198. */
  199. protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
  200. return new AbstractActionPropertyChangeListener(this, a) {
  201. public void propertyChange(PropertyChangeEvent e) {
  202. String propertyName = e.getPropertyName();
  203. AbstractButton button = (AbstractButton)getTarget();
  204. if (button == null) { //WeakRef GC'ed in 1.2
  205. Action action = (Action)e.getSource();
  206. action.removePropertyChangeListener(this);
  207. } else {
  208. if (e.getPropertyName().equals(Action.NAME)) {
  209. String text = (String) e.getNewValue();
  210. button.setText(text);
  211. button.repaint();
  212. } else if (e.getPropertyName().equals(Action.SHORT_DESCRIPTION)) {
  213. String text = (String) e.getNewValue();
  214. button.setToolTipText(text);
  215. } else if (propertyName.equals("enabled")) {
  216. Boolean enabledState = (Boolean) e.getNewValue();
  217. button.setEnabled(enabledState.booleanValue());
  218. button.repaint();
  219. }
  220. }
  221. }
  222. };
  223. }
  224. /**
  225. * See readObject() and writeObject() in JComponent for more
  226. * information about serialization in Swing.
  227. */
  228. private void writeObject(ObjectOutputStream s) throws IOException {
  229. s.defaultWriteObject();
  230. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  231. ui.installUI(this);
  232. }
  233. }
  234. /**
  235. * Returns a string representation of this JRadioButton. This method
  236. * is intended to be used only for debugging purposes, and the
  237. * content and format of the returned string may vary between
  238. * implementations. The returned string may be empty but may not
  239. * be <code>null</code>.
  240. *
  241. * @return a string representation of this JRadioButton.
  242. */
  243. protected String paramString() {
  244. return super.paramString();
  245. }
  246. /////////////////
  247. // Accessibility support
  248. ////////////////
  249. /**
  250. * Gets the AccessibleContext associated with this JRadioButton.
  251. * For JRadioButtons, the AccessibleContext takes the form of an
  252. * AccessibleJRadioButton.
  253. * A new AccessibleJRadioButton instance is created if necessary.
  254. *
  255. * @return an AccessibleJRadioButton that serves as the
  256. * AccessibleContext of this JRadioButton
  257. * @beaninfo
  258. * expert: true
  259. * description: The AccessibleContext associated with this Button
  260. */
  261. public AccessibleContext getAccessibleContext() {
  262. if (accessibleContext == null) {
  263. accessibleContext = new AccessibleJRadioButton();
  264. }
  265. return accessibleContext;
  266. }
  267. /**
  268. * This class implements accessibility support for the
  269. * <code>JRadioButton</code> class. It provides an implementation of the
  270. * Java Accessibility API appropriate to radio button
  271. * user-interface elements.
  272. * <p>
  273. * <strong>Warning:</strong>
  274. * Serialized objects of this class will not be compatible with
  275. * future Swing releases. The current serialization support is appropriate
  276. * for short term storage or RMI between applications running the same
  277. * version of Swing. A future release of Swing will provide support for
  278. * long term persistence.
  279. */
  280. protected class AccessibleJRadioButton extends AccessibleJToggleButton {
  281. /**
  282. * Get the role of this object.
  283. *
  284. * @return an instance of AccessibleRole describing the role of the object
  285. * @see AccessibleRole
  286. */
  287. public AccessibleRole getAccessibleRole() {
  288. return AccessibleRole.RADIO_BUTTON;
  289. }
  290. } // inner class AccessibleJRadioButton
  291. }