1. /*
  2. * @(#)JRadioButtonMenuItem.java 1.34 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.util.EventListener;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.awt.image.*;
  12. import java.io.ObjectOutputStream;
  13. import java.io.ObjectInputStream;
  14. import java.io.IOException;
  15. import javax.swing.plaf.*;
  16. import javax.accessibility.*;
  17. /**
  18. * An implementation of a RadioButtonMenuItem. A RadioButtonMenuItem is
  19. * a menu item that is part of a group of menu items in which only one
  20. * item in the group can be selected. The selected item displays its
  21. * selected state. Selecting it causes any other selected item to
  22. * switch to the unselected state.
  23. * <p>
  24. * Used with a {@link ButtonGroup} object to create a group of menu items
  25. * in which only one item at a time can be selected. (Create a ButtonGroup
  26. * object and use its <code>add</code> method to include the JRadioButtonMenuItem
  27. * objects in the group.)
  28. * <p>
  29. * For the keyboard keys used by this component in the standard Look and
  30. * Feel (L&F) renditions, see the
  31. * <a href="doc-files/Key-Index.html#JRadioButtonMenuItem">JRadioButtonMenuItem</a> key assignments.
  32. * <p>
  33. * <strong>Warning:</strong>
  34. * Serialized objects of this class will not be compatible with
  35. * future Swing releases. The current serialization support is appropriate
  36. * for short term storage or RMI between applications running the same
  37. * version of Swing. A future release of Swing will provide support for
  38. * long term persistence.
  39. *
  40. * @beaninfo
  41. * attribute: isContainer false
  42. *
  43. * @version 1.34 11/29/01
  44. * @author Georges Saab
  45. * @author David Karlton
  46. * @see ButtonGroup
  47. */
  48. public class JRadioButtonMenuItem extends JMenuItem implements Accessible {
  49. /**
  50. * @see #getUIClassID
  51. * @see #readObject
  52. */
  53. private static final String uiClassID = "RadioButtonMenuItemUI";
  54. /**
  55. * Creates a JRadioButtonMenuItem with no set text or icon.
  56. */
  57. public JRadioButtonMenuItem() {
  58. this(null, null, false);
  59. }
  60. /**
  61. * Creates a JRadioButtonMenuItem with an icon.
  62. *
  63. * @param icon the Icon to display on the RadioButtonMenuItem.
  64. */
  65. public JRadioButtonMenuItem(Icon icon) {
  66. this(null, icon, false);
  67. }
  68. /**
  69. * Creates a JRadioButtonMenuItem with text.
  70. *
  71. * @param text the text of the RadioButtonMenuItem.
  72. */
  73. public JRadioButtonMenuItem(String text) {
  74. this(text, null, false);
  75. }
  76. /**
  77. * Creates a JRadioButtonMenuItem with the specified text
  78. * and Icon.
  79. *
  80. * @param text the text of the RadioButtonMenuItem
  81. * @param icon the icon to display on the RadioButtonMenuItem
  82. */
  83. public JRadioButtonMenuItem(String text, Icon icon) {
  84. this(text, icon, false);
  85. }
  86. /**
  87. * Creates a radiobutton menu item with the specified text
  88. * and selection state.
  89. *
  90. * @param text the text of the CheckBoxMenuItem.
  91. * @param b the selected state of the checkboxmenuitem
  92. */
  93. public JRadioButtonMenuItem(String text, boolean b) {
  94. this(text);
  95. setSelected(b);
  96. }
  97. /**
  98. * Creates a radio button menu item with the specified image
  99. * and selection state, but no text.
  100. *
  101. * @param icon the image that the button should display
  102. * @param selected if true, the button is initially selected;
  103. * otherwise, the button is initially unselected
  104. */
  105. public JRadioButtonMenuItem(Icon icon, boolean selected) {
  106. this(null, icon, selected);
  107. }
  108. /**
  109. * Creates a radio button menu item that has the specified
  110. * text, image, and selection state.
  111. *
  112. * @param text the string displayed on the radio button
  113. * @param icon the image that the button should display
  114. */
  115. public JRadioButtonMenuItem(String text, Icon icon, boolean selected) {
  116. super(text, icon);
  117. setModel(new JToggleButton.ToggleButtonModel());
  118. setSelected(selected);
  119. }
  120. /**
  121. * Returns the name of the L&F class that renders this component.
  122. *
  123. * @return "RadioButtonMenuItemUI"
  124. * @see JComponent#getUIClassID
  125. * @see UIDefaults#getUI
  126. */
  127. public String getUIClassID() {
  128. return uiClassID;
  129. }
  130. /**
  131. * Override Component.requestFocus() to not grab focus.
  132. */
  133. public void requestFocus() {}
  134. /**
  135. * See readObject() and writeObject() in JComponent for more
  136. * information about serialization in Swing.
  137. */
  138. private void writeObject(ObjectOutputStream s) throws IOException {
  139. s.defaultWriteObject();
  140. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  141. ui.installUI(this);
  142. }
  143. }
  144. /**
  145. * Returns a string representation of this JRadioButtonMenuItem.
  146. * This method
  147. * is intended to be used only for debugging purposes, and the
  148. * content and format of the returned string may vary between
  149. * implementations. The returned string may be empty but may not
  150. * be <code>null</code>.
  151. *
  152. * @return a string representation of this JRadioButtonMenuItem.
  153. */
  154. protected String paramString() {
  155. return super.paramString();
  156. }
  157. /////////////////
  158. // Accessibility support
  159. ////////////////
  160. /**
  161. * Get the AccessibleContext associated with this JComponent
  162. *
  163. * @return the AccessibleContext of this JComponent
  164. */
  165. public AccessibleContext getAccessibleContext() {
  166. if (accessibleContext == null) {
  167. accessibleContext = new AccessibleJRadioButtonMenuItem();
  168. }
  169. return accessibleContext;
  170. }
  171. /**
  172. * The class used to obtain the accessible role for this object.
  173. * <p>
  174. * <strong>Warning:</strong>
  175. * Serialized objects of this class will not be compatible with
  176. * future Swing releases. The current serialization support is appropriate
  177. * for short term storage or RMI between applications running the same
  178. * version of Swing. A future release of Swing will provide support for
  179. * long term persistence.
  180. */
  181. protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem {
  182. /**
  183. * Get the role of this object.
  184. *
  185. * @return an instance of AccessibleRole describing the role of the
  186. * object
  187. */
  188. public AccessibleRole getAccessibleRole() {
  189. return AccessibleRole.RADIO_BUTTON;
  190. }
  191. } // inner class AccessibleJRadioButtonMenuItem
  192. }