1. /*
  2. * @(#)JRadioButtonMenuItem.java 1.42 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.util.EventListener;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.awt.image.*;
  15. import java.io.ObjectOutputStream;
  16. import java.io.ObjectInputStream;
  17. import java.io.IOException;
  18. import javax.swing.plaf.*;
  19. import javax.accessibility.*;
  20. /**
  21. * An implementation of a radio button menu item.
  22. * A <code>JRadioButtonMenuItem</code> is
  23. * a menu item that is part of a group of menu items in which only one
  24. * item in the group can be selected. The selected item displays its
  25. * selected state. Selecting it causes any other selected item to
  26. * switch to the unselected state.
  27. * To control the selected state of a group of radio button menu items,
  28. * use a <code>ButtonGroup</code> object.
  29. * <p>
  30. * For further documentation and examples see
  31. * <a
  32. href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
  33. * a section in <em>The Java Tutorial.</em>
  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#JRadioButtonMenuItem">JRadioButtonMenuItem</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. * description: A component within a group of menu items which can be selected.
  48. *
  49. * @version 1.42 04/06/00
  50. * @author Georges Saab
  51. * @author David Karlton
  52. * @see ButtonGroup
  53. */
  54. public class JRadioButtonMenuItem extends JMenuItem implements Accessible {
  55. /**
  56. * @see #getUIClassID
  57. * @see #readObject
  58. */
  59. private static final String uiClassID = "RadioButtonMenuItemUI";
  60. /**
  61. * Creates a <code>JRadioButtonMenuItem</code> with no set text or icon.
  62. */
  63. public JRadioButtonMenuItem() {
  64. this(null, null, false);
  65. }
  66. /**
  67. * Creates a <code>JRadioButtonMenuItem</code> with an icon.
  68. *
  69. * @param icon the <code>Icon</code> to display on the
  70. * <code>JRadioButtonMenuItem</code>
  71. */
  72. public JRadioButtonMenuItem(Icon icon) {
  73. this(null, icon, false);
  74. }
  75. /**
  76. * Creates a <code>JRadioButtonMenuItem</code> with text.
  77. *
  78. * @param text the text of the <code>JRadioButtonMenuItem</code>
  79. */
  80. public JRadioButtonMenuItem(String text) {
  81. this(text, null, false);
  82. }
  83. /**
  84. * Creates a radio button menu item whose properties are taken from the
  85. * <code>Action</code> supplied.
  86. *
  87. * @param a the <code>Action</code> on which to base the radio
  88. * button menu item
  89. *
  90. * @since 1.3
  91. */
  92. public JRadioButtonMenuItem(Action a) {
  93. this();
  94. setAction(a);
  95. }
  96. /**
  97. * Creates a radio button menu item with the specified text
  98. * and <code>Icon</code>.
  99. *
  100. * @param text the text of the <code>JRadioButtonMenuItem</code>
  101. * @param icon the icon to display on the <code>JRadioButtonMenuItem</code>
  102. */
  103. public JRadioButtonMenuItem(String text, Icon icon) {
  104. this(text, icon, false);
  105. }
  106. /**
  107. * Creates a radio button menu item with the specified text
  108. * and selection state.
  109. *
  110. * @param text the text of the <code>CheckBoxMenuItem</code>
  111. * @param selected the selected state of the <code>CheckBoxMenuItem</code>
  112. */
  113. public JRadioButtonMenuItem(String text, boolean selected) {
  114. this(text);
  115. setSelected(selected);
  116. }
  117. /**
  118. * Creates a radio button menu item with the specified image
  119. * and selection state, but no text.
  120. *
  121. * @param icon the image that the button should display
  122. * @param selected if true, the button is initially selected;
  123. * otherwise, the button is initially unselected
  124. */
  125. public JRadioButtonMenuItem(Icon icon, boolean selected) {
  126. this(null, icon, selected);
  127. }
  128. /**
  129. * Creates a radio button menu item that has the specified
  130. * text, image, and selection state. All other constructors
  131. * defer to this one.
  132. *
  133. * @param text the string displayed on the radio button
  134. * @param icon the image that the button should display
  135. */
  136. public JRadioButtonMenuItem(String text, Icon icon, boolean selected) {
  137. super(text, icon);
  138. setModel(new JToggleButton.ToggleButtonModel());
  139. setSelected(selected);
  140. }
  141. /**
  142. * Returns the name of the L&F class that renders this component.
  143. *
  144. * @return the string "RadioButtonMenuItemUI"
  145. * @see JComponent#getUIClassID
  146. * @see UIDefaults#getUI
  147. */
  148. public String getUIClassID() {
  149. return uiClassID;
  150. }
  151. /**
  152. * Overrides <code>Component.requestFocus</code> to not grab focus.
  153. */
  154. public void requestFocus() {}
  155. /**
  156. * See <code>readObject</code> and <code>writeObject</code> in
  157. * <code>JComponent</code> for more
  158. * information about serialization in Swing.
  159. */
  160. private void writeObject(ObjectOutputStream s) throws IOException {
  161. s.defaultWriteObject();
  162. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  163. ui.installUI(this);
  164. }
  165. }
  166. /**
  167. * Returns a string representation of this
  168. * <code>JRadioButtonMenuItem</code>. This method
  169. * is intended to be used only for debugging purposes, and the
  170. * content and format of the returned string may vary between
  171. * implementations. The returned string may be empty but may not
  172. * be <code>null</code>.
  173. *
  174. * @return a string representation of this
  175. * <code>JRadioButtonMenuItem</code>
  176. */
  177. protected String paramString() {
  178. return super.paramString();
  179. }
  180. /////////////////
  181. // Accessibility support
  182. ////////////////
  183. /**
  184. * Gets the AccessibleContext associated with this JRadioButtonMenuItem.
  185. * For JRadioButtonMenuItems, the AccessibleContext takes the form of an
  186. * AccessibleJRadioButtonMenuItem.
  187. * A new AccessibleJRadioButtonMenuItem instance is created if necessary.
  188. *
  189. * @return an AccessibleJRadioButtonMenuItem that serves as the
  190. * AccessibleContext of this JRadioButtonMenuItem
  191. */
  192. public AccessibleContext getAccessibleContext() {
  193. if (accessibleContext == null) {
  194. accessibleContext = new AccessibleJRadioButtonMenuItem();
  195. }
  196. return accessibleContext;
  197. }
  198. /**
  199. * This class implements accessibility support for the
  200. * <code>JRadioButtonMenuItem</code> class. It provides an
  201. * implementation of the Java Accessibility API appropriate to
  202. * <code>JRadioButtonMenuItem</code> user-interface elements.
  203. * <p>
  204. * <strong>Warning:</strong>
  205. * Serialized objects of this class will not be compatible with
  206. * future Swing releases. The current serialization support is appropriate
  207. * for short term storage or RMI between applications running the same
  208. * version of Swing. A future release of Swing will provide support for
  209. * long term persistence.
  210. */
  211. protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem {
  212. /**
  213. * Get the role of this object.
  214. *
  215. * @return an instance of AccessibleRole describing the role of the
  216. * object
  217. */
  218. public AccessibleRole getAccessibleRole() {
  219. return AccessibleRole.RADIO_BUTTON;
  220. }
  221. } // inner class AccessibleJRadioButtonMenuItem
  222. }