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