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