1. /*
  2. * @(#)JCheckBox.java 1.47 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 CheckBox -- an item that can be selected or
  17. * deselected, and which displays its state to the user. In a group
  18. * of checkboxes, multiple checkboxes can be selected.
  19. *
  20. * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/checkbox.html">How to Use CheckBoxes</a>
  21. * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  22. * for further documentation.
  23. * <p>
  24. * For the keyboard keys used by this component in the standard Look and
  25. * Feel (L&F) renditions, see the
  26. * <a href="doc-files/Key-Index.html#JCheckBox">JCheckBox</a> key assignments.
  27. * <p>
  28. * <strong>Warning:</strong>
  29. * Serialized objects of this class will not be compatible with
  30. * future Swing releases. The current serialization support is appropriate
  31. * for short term storage or RMI between applications running the same
  32. * version of Swing. A future release of Swing will provide support for
  33. * long term persistence.
  34. *
  35. * @see JRadioButton
  36. *
  37. * @beaninfo
  38. * attribute: isContainer false
  39. *
  40. * @version 1.47 11/29/01
  41. * @author Jeff Dinkins
  42. */
  43. public class JCheckBox extends JToggleButton implements Accessible {
  44. /**
  45. * @see #getUIClassID
  46. * @see #readObject
  47. */
  48. private static final String uiClassID = "CheckBoxUI";
  49. /**
  50. * Creates an initially unselected checkbox button with no text, no icon.
  51. */
  52. public JCheckBox () {
  53. this(null, null, false);
  54. }
  55. /**
  56. * Creates an initially unselected checkbox with an icon.
  57. *
  58. * @param icon the Icon image to display
  59. */
  60. public JCheckBox(Icon icon) {
  61. this(null, icon, false);
  62. }
  63. /**
  64. * Creates a checkbox with an icon and specifies whether
  65. * or not it is initially selected.
  66. *
  67. * @param icon the Icon image to display
  68. * @param selected a boolean value indicating the initial selection
  69. * state. If <code>true</code> the checkbox is selected
  70. */
  71. public JCheckBox(Icon icon, boolean selected) {
  72. this(null, icon, selected);
  73. }
  74. /**
  75. * Creates an initially unselected checkbox with text.
  76. *
  77. * @param text the text of the checkbox.
  78. */
  79. public JCheckBox (String text) {
  80. this(text, null, false);
  81. }
  82. /**
  83. * Creates a checkbox with text and specifies whether
  84. * or not it is initially selected.
  85. *
  86. * @param text the text of the checkbox.
  87. * @param selected a boolean value indicating the initial selection
  88. * state. If <code>true</code> the checkbox is selected
  89. */
  90. public JCheckBox (String text, boolean selected) {
  91. this(text, null, selected);
  92. }
  93. /**
  94. * Creates an initially unselected checkbox with
  95. * the specified text and icon.
  96. *
  97. * @param text the text of the checkbox.
  98. * @param icon the Icon image to display
  99. */
  100. public JCheckBox(String text, Icon icon) {
  101. this(text, icon, false);
  102. }
  103. /**
  104. * Creates a checkbox with text and icon,
  105. * and specifies whether or not it is initially selected.
  106. *
  107. * @param text the text of the checkbox.
  108. * @param icon the Icon image to display
  109. * @param selected a boolean value indicating the initial selection
  110. * state. If <code>true</code> the checkbox is selected
  111. */
  112. public JCheckBox (String text, Icon icon, boolean selected) {
  113. super(text, icon, selected);
  114. setBorderPainted(false);
  115. setHorizontalAlignment(LEFT);
  116. }
  117. /**
  118. * Notification from the UIFactory that the L&F
  119. * has changed.
  120. *
  121. * @see JComponent#updateUI
  122. */
  123. public void updateUI() {
  124. setUI((ButtonUI)UIManager.getUI(this));
  125. }
  126. /**
  127. * Returns a string that specifies the name of the L&F class
  128. * that renders this component.
  129. *
  130. * @return "CheckBoxUI"
  131. * @see JComponent#getUIClassID
  132. * @see UIDefaults#getUI
  133. * @beaninfo
  134. * expert: true
  135. * description: A string that specifies the name of the L&F class
  136. */
  137. public String getUIClassID() {
  138. return uiClassID;
  139. }
  140. /**
  141. * See JComponent.readObject() for information about serialization
  142. * in Swing.
  143. */
  144. private void readObject(ObjectInputStream s)
  145. throws IOException, ClassNotFoundException
  146. {
  147. s.defaultReadObject();
  148. if (getUIClassID().equals(uiClassID)) {
  149. updateUI();
  150. }
  151. }
  152. /**
  153. * Returns a string representation of this JCheckBox. This method
  154. * is intended to be used only for debugging purposes, and the
  155. * content and format of the returned string may vary between
  156. * implementations. The returned string may be empty but may not
  157. * be <code>null</code>.
  158. * specific new aspects of the JFC components.
  159. *
  160. * @return a string representation of this JCheckBox.
  161. */
  162. protected String paramString() {
  163. return super.paramString();
  164. }
  165. /////////////////
  166. // Accessibility support
  167. ////////////////
  168. /**
  169. * Get the AccessibleContext associated with this JComponent
  170. *
  171. * @return the AccessibleContext of this JComponent
  172. * @beaninfo
  173. * expert: true
  174. * description: The AccessibleContext associated with this CheckBox.
  175. */
  176. public AccessibleContext getAccessibleContext() {
  177. if (accessibleContext == null) {
  178. accessibleContext = new AccessibleJCheckBox();
  179. }
  180. return accessibleContext;
  181. }
  182. /**
  183. * The class used to obtain the accessible role for this object.
  184. * <p>
  185. * <strong>Warning:</strong>
  186. * Serialized objects of this class will not be compatible with
  187. * future Swing releases. The current serialization support is appropriate
  188. * for short term storage or RMI between applications running the same
  189. * version of Swing. A future release of Swing will provide support for
  190. * long term persistence.
  191. */
  192. protected class AccessibleJCheckBox extends AccessibleJToggleButton {
  193. /**
  194. * Get the role of this object.
  195. *
  196. * @return an instance of AccessibleRole describing the role of the object
  197. * @see AccessibleRole
  198. */
  199. public AccessibleRole getAccessibleRole() {
  200. return AccessibleRole.CHECK_BOX;
  201. }
  202. } // inner class AccessibleJCheckBox
  203. }