1. /*
  2. * @(#)JCheckBox.java 1.59 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 check box -- an item that can be selected or
  21. * deselected, and which displays its state to the user.
  22. * By convention, any number of check boxes in a group can be selected.
  23. * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttonc, Check Boxes, and Radio Buttons</a>
  24. * in <em>The Java Tutorial</em>
  25. * for examples and information on using check boxes.
  26. * <p>
  27. * For the keyboard keys used by this component in the standard Look and
  28. * Feel (L&F) renditions, see the
  29. * <a href="doc-files/Key-Index.html#JCheckBox">JCheckBox</a> key assignments.
  30. * <p>
  31. * <strong>Warning:</strong>
  32. * Serialized objects of this class will not be compatible with
  33. * future Swing releases. The current serialization support is appropriate
  34. * for short term storage or RMI between applications running the same
  35. * version of Swing. A future release of Swing will provide support for
  36. * long term persistence.
  37. *
  38. * @see JRadioButton
  39. *
  40. * @beaninfo
  41. * attribute: isContainer false
  42. * description: A component which can be selected or deselected.
  43. *
  44. * @version 1.59 04/06/00
  45. * @author Jeff Dinkins
  46. */
  47. public class JCheckBox extends JToggleButton implements Accessible {
  48. /** Identifies a change to the flat property. */
  49. public static final String BORDER_PAINTED_FLAT_CHANGED_PROPERTY = "borderPaintedFlat";
  50. private boolean flat = false;
  51. /**
  52. * @see #getUIClassID
  53. * @see #readObject
  54. */
  55. private static final String uiClassID = "CheckBoxUI";
  56. /**
  57. * Creates an initially unselected check box button with no text, no icon.
  58. */
  59. public JCheckBox () {
  60. this(null, null, false);
  61. }
  62. /**
  63. * Creates an initially unselected check box with an icon.
  64. *
  65. * @param icon the Icon image to display
  66. */
  67. public JCheckBox(Icon icon) {
  68. this(null, icon, false);
  69. }
  70. /**
  71. * Creates a check box with an icon and specifies whether
  72. * or not it is initially selected.
  73. *
  74. * @param icon the Icon image to display
  75. * @param selected a boolean value indicating the initial selection
  76. * state. If <code>true</code> the check box is selected
  77. */
  78. public JCheckBox(Icon icon, boolean selected) {
  79. this(null, icon, selected);
  80. }
  81. /**
  82. * Creates an initially unselected check box with text.
  83. *
  84. * @param text the text of the check box.
  85. */
  86. public JCheckBox (String text) {
  87. this(text, null, false);
  88. }
  89. /**
  90. * Creates a check box where properties are taken from the
  91. * Action supplied.
  92. *
  93. * @since 1.3
  94. */
  95. public JCheckBox(Action a) {
  96. this();
  97. setAction(a);
  98. }
  99. /**
  100. * Creates a check box with text and specifies whether
  101. * or not it is initially selected.
  102. *
  103. * @param text the text of the check box.
  104. * @param selected a boolean value indicating the initial selection
  105. * state. If <code>true</code> the check box is selected
  106. */
  107. public JCheckBox (String text, boolean selected) {
  108. this(text, null, selected);
  109. }
  110. /**
  111. * Creates an initially unselected check box with
  112. * the specified text and icon.
  113. *
  114. * @param text the text of the check box.
  115. * @param icon the Icon image to display
  116. */
  117. public JCheckBox(String text, Icon icon) {
  118. this(text, icon, false);
  119. }
  120. /**
  121. * Creates a check box with text and icon,
  122. * and specifies whether or not it is initially selected.
  123. *
  124. * @param text the text of the check box.
  125. * @param icon the Icon image to display
  126. * @param selected a boolean value indicating the initial selection
  127. * state. If <code>true</code> the check box is selected
  128. */
  129. public JCheckBox (String text, Icon icon, boolean selected) {
  130. super(text, icon, selected);
  131. setBorderPainted(false);
  132. setHorizontalAlignment(LEFT);
  133. }
  134. /**
  135. * Sets whether the border should be painted flat. This is usually
  136. * set to true when a JCheckBox instance is used as a renderer in a
  137. * component such as a JTable or JTree.
  138. *
  139. * @param b if true, the border is painted flat.
  140. * @see #isBorderPaintedFlat
  141. * @beaninfo
  142. * bound: true
  143. * attribute: visualUpdate true
  144. * description: Whether the border is painted flat.
  145. */
  146. public void setBorderPaintedFlat(boolean b) {
  147. flat = b;
  148. boolean oldValue = flat;
  149. flat = b;
  150. firePropertyChange(BORDER_PAINTED_FLAT_CHANGED_PROPERTY, oldValue, flat);
  151. if (b != oldValue) {
  152. revalidate();
  153. repaint();
  154. }
  155. }
  156. /**
  157. * Returns whether the border should be painted flat.
  158. * @see #setBorderPaintedFlat
  159. */
  160. public boolean isBorderPaintedFlat() {
  161. return flat;
  162. }
  163. /**
  164. * Notification from the UIFactory that the L&F
  165. * has changed.
  166. *
  167. * @see JComponent#updateUI
  168. */
  169. public void updateUI() {
  170. setUI((ButtonUI)UIManager.getUI(this));
  171. }
  172. /**
  173. * Returns a string that specifies the name of the L&F class
  174. * that renders this component.
  175. *
  176. * @return "CheckBoxUI"
  177. * @see JComponent#getUIClassID
  178. * @see UIDefaults#getUI
  179. * @beaninfo
  180. * expert: true
  181. * description: A string that specifies the name of the L&F class
  182. */
  183. public String getUIClassID() {
  184. return uiClassID;
  185. }
  186. /**
  187. * Factory method which sets the ActionEvent source's properties
  188. * according to values from the Action instance. The properties
  189. * which are set may differ for subclasses.
  190. * By default, the properties which get set are Text, Icon
  191. * Enabled, and ToolTipText.
  192. *
  193. * @param a the Action from which to get the properties, or null
  194. * @since 1.3
  195. * @see Action
  196. * @see #setAction
  197. */
  198. protected void configurePropertiesFromAction(Action a) {
  199. setText((a!=null?(String)a.getValue(Action.NAME):null));
  200. setEnabled((a!=null?a.isEnabled():true));
  201. setToolTipText((a!=null?(String)a.getValue(Action.SHORT_DESCRIPTION):null));
  202. }
  203. /**
  204. * Factory method which creates the PropertyChangeListener
  205. * used to update the ActionEvent source as properties change on
  206. * its Action instance. Subclasses may override this in order
  207. * to provide their own PropertyChangeListener if the set of
  208. * properties which should be kept up to date differs from the
  209. * default properties (Text, Icon, Enabled, ToolTipText).
  210. *
  211. * Note that PropertyChangeListeners should avoid holding
  212. * strong references to the ActionEvent source, as this may hinder
  213. * garbage collection of the ActionEvent source and all components
  214. * in its containment hierarchy.
  215. *
  216. * @since 1.3
  217. * @see Action
  218. * @see #setAction
  219. */
  220. protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
  221. return new AbstractActionPropertyChangeListener(this, a) {
  222. public void propertyChange(PropertyChangeEvent e) {
  223. String propertyName = e.getPropertyName();
  224. AbstractButton button = (AbstractButton)getTarget();
  225. if (button == null) { //WeakRef GC'ed in 1.2
  226. Action action = (Action)e.getSource();
  227. action.removePropertyChangeListener(this);
  228. } else {
  229. if (e.getPropertyName().equals(Action.NAME)) {
  230. String text = (String) e.getNewValue();
  231. button.setText(text);
  232. button.repaint();
  233. } else if (e.getPropertyName().equals(Action.SHORT_DESCRIPTION)) {
  234. String text = (String) e.getNewValue();
  235. button.setToolTipText(text);
  236. } else if (propertyName.equals("enabled")) {
  237. Boolean enabledState = (Boolean) e.getNewValue();
  238. button.setEnabled(enabledState.booleanValue());
  239. button.repaint();
  240. }
  241. }
  242. }
  243. };
  244. }
  245. /**
  246. * See JComponent.readObject() for information about serialization
  247. * in Swing.
  248. */
  249. private void readObject(ObjectInputStream s)
  250. throws IOException, ClassNotFoundException
  251. {
  252. s.defaultReadObject();
  253. if (getUIClassID().equals(uiClassID)) {
  254. updateUI();
  255. }
  256. }
  257. /**
  258. * Returns a string representation of this JCheckBox. This method
  259. * is intended to be used only for debugging purposes, and the
  260. * content and format of the returned string may vary between
  261. * implementations. The returned string may be empty but may not
  262. * be <code>null</code>.
  263. * specific new aspects of the JFC components.
  264. *
  265. * @return a string representation of this JCheckBox.
  266. */
  267. protected String paramString() {
  268. return super.paramString();
  269. }
  270. /////////////////
  271. // Accessibility support
  272. ////////////////
  273. /**
  274. * Gets the AccessibleContext associated with this JCheckBox.
  275. * For JCheckBoxes, the AccessibleContext takes the form of an
  276. * AccessibleJCheckBox.
  277. * A new AccessibleJCheckBox instance is created if necessary.
  278. *
  279. * @return an AccessibleJCheckBox that serves as the
  280. * AccessibleContext of this JCheckBox
  281. * @beaninfo
  282. * expert: true
  283. * description: The AccessibleContext associated with this CheckBox.
  284. */
  285. public AccessibleContext getAccessibleContext() {
  286. if (accessibleContext == null) {
  287. accessibleContext = new AccessibleJCheckBox();
  288. }
  289. return accessibleContext;
  290. }
  291. /**
  292. * This class implements accessibility support for the
  293. * <code>JCheckBox</code> class. It provides an implementation of the
  294. * Java Accessibility API appropriate to check box user-interface
  295. * elements.
  296. * <p>
  297. * <strong>Warning:</strong>
  298. * Serialized objects of this class will not be compatible with
  299. * future Swing releases. The current serialization support is appropriate
  300. * for short term storage or RMI between applications running the same
  301. * version of Swing. A future release of Swing will provide support for
  302. * long term persistence.
  303. */
  304. protected class AccessibleJCheckBox extends AccessibleJToggleButton {
  305. /**
  306. * Get the role of this object.
  307. *
  308. * @return an instance of AccessibleRole describing the role of the object
  309. * @see AccessibleRole
  310. */
  311. public AccessibleRole getAccessibleRole() {
  312. return AccessibleRole.CHECK_BOX;
  313. }
  314. } // inner class AccessibleJCheckBox
  315. }