001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing;
026
027 import java.awt.*;
028 import java.awt.event.*;
029 import java.beans.*;
030
031 import javax.swing.plaf.*;
032 import javax.accessibility.*;
033
034 import java.io.ObjectOutputStream;
035 import java.io.ObjectInputStream;
036 import java.io.IOException;
037
038 /**
039 * An implementation of a check box -- an item that can be selected or
040 * deselected, and which displays its state to the user.
041 * By convention, any number of check boxes in a group can be selected.
042 * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
043 * in <em>The Java Tutorial</em>
044 * for examples and information on using check boxes.
045 * <p>
046 * Buttons can be configured, and to some degree controlled, by
047 * <code><a href="Action.html">Action</a></code>s. Using an
048 * <code>Action</code> with a button has many benefits beyond directly
049 * configuring a button. Refer to <a href="Action.html#buttonActions">
050 * Swing Components Supporting <code>Action</code></a> for more
051 * details, and you can find more information in <a
052 * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
053 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
054 * <p>
055 * <strong>Warning:</strong> Swing is not thread safe. For more
056 * information see <a
057 * href="package-summary.html#threading">Swing's Threading
058 * Policy</a>.
059 * <p>
060 * <strong>Warning:</strong>
061 * Serialized objects of this class will not be compatible with
062 * future Swing releases. The current serialization support is
063 * appropriate for short term storage or RMI between applications running
064 * the same version of Swing. As of 1.4, support for long term storage
065 * of all JavaBeans<sup><font size="-2">TM</font></sup>
066 * has been added to the <code>java.beans</code> package.
067 * Please see {@link java.beans.XMLEncoder}.
068 *
069 * @see JRadioButton
070 *
071 * @beaninfo
072 * attribute: isContainer false
073 * description: A component which can be selected or deselected.
074 *
075 * @version 1.84 05/05/07
076 * @author Jeff Dinkins
077 */
078 public class JCheckBox extends JToggleButton implements Accessible {
079
080 /** Identifies a change to the flat property. */
081 public static final String BORDER_PAINTED_FLAT_CHANGED_PROPERTY = "borderPaintedFlat";
082
083 private boolean flat = false;
084
085 /**
086 * @see #getUIClassID
087 * @see #readObject
088 */
089 private static final String uiClassID = "CheckBoxUI";
090
091 /**
092 * Creates an initially unselected check box button with no text, no icon.
093 */
094 public JCheckBox() {
095 this (null, null, false);
096 }
097
098 /**
099 * Creates an initially unselected check box with an icon.
100 *
101 * @param icon the Icon image to display
102 */
103 public JCheckBox(Icon icon) {
104 this (null, icon, false);
105 }
106
107 /**
108 * Creates a check box with an icon and specifies whether
109 * or not it is initially selected.
110 *
111 * @param icon the Icon image to display
112 * @param selected a boolean value indicating the initial selection
113 * state. If <code>true</code> the check box is selected
114 */
115 public JCheckBox(Icon icon, boolean selected) {
116 this (null, icon, selected);
117 }
118
119 /**
120 * Creates an initially unselected check box with text.
121 *
122 * @param text the text of the check box.
123 */
124 public JCheckBox(String text) {
125 this (text, null, false);
126 }
127
128 /**
129 * Creates a check box where properties are taken from the
130 * Action supplied.
131 *
132 * @since 1.3
133 */
134 public JCheckBox(Action a) {
135 this ();
136 setAction(a);
137 }
138
139 /**
140 * Creates a check box with text and specifies whether
141 * or not it is initially selected.
142 *
143 * @param text the text of the check box.
144 * @param selected a boolean value indicating the initial selection
145 * state. If <code>true</code> the check box is selected
146 */
147 public JCheckBox(String text, boolean selected) {
148 this (text, null, selected);
149 }
150
151 /**
152 * Creates an initially unselected check box with
153 * the specified text and icon.
154 *
155 * @param text the text of the check box.
156 * @param icon the Icon image to display
157 */
158 public JCheckBox(String text, Icon icon) {
159 this (text, icon, false);
160 }
161
162 /**
163 * Creates a check box with text and icon,
164 * and specifies whether or not it is initially selected.
165 *
166 * @param text the text of the check box.
167 * @param icon the Icon image to display
168 * @param selected a boolean value indicating the initial selection
169 * state. If <code>true</code> the check box is selected
170 */
171 public JCheckBox(String text, Icon icon, boolean selected) {
172 super (text, icon, selected);
173 setUIProperty("borderPainted", Boolean.FALSE);
174 setHorizontalAlignment(LEADING);
175 }
176
177 /**
178 * Sets the <code>borderPaintedFlat</code> property,
179 * which gives a hint to the look and feel as to the
180 * appearance of the check box border.
181 * This is usually set to <code>true</code> when a
182 * <code>JCheckBox</code> instance is used as a
183 * renderer in a component such as a <code>JTable</code> or
184 * <code>JTree</code>. The default value for the
185 * <code>borderPaintedFlat</code> property is <code>false</code>.
186 * This method fires a property changed event.
187 * Some look and feels might not implement flat borders;
188 * they will ignore this property.
189 *
190 * @param b <code>true</code> requests that the border be painted flat;
191 * <code>false</code> requests normal borders
192 * @see #isBorderPaintedFlat
193 * @beaninfo
194 * bound: true
195 * attribute: visualUpdate true
196 * description: Whether the border is painted flat.
197 * @since 1.3
198 */
199 public void setBorderPaintedFlat(boolean b) {
200 boolean oldValue = flat;
201 flat = b;
202 firePropertyChange(BORDER_PAINTED_FLAT_CHANGED_PROPERTY,
203 oldValue, flat);
204 if (b != oldValue) {
205 revalidate();
206 repaint();
207 }
208 }
209
210 /**
211 * Gets the value of the <code>borderPaintedFlat</code> property.
212 *
213 * @return the value of the <code>borderPaintedFlat</code> property
214 * @see #setBorderPaintedFlat
215 * @since 1.3
216 */
217 public boolean isBorderPaintedFlat() {
218 return flat;
219 }
220
221 /**
222 * Resets the UI property to a value from the current look and feel.
223 *
224 * @see JComponent#updateUI
225 */
226 public void updateUI() {
227 setUI((ButtonUI) UIManager.getUI(this ));
228 }
229
230 /**
231 * Returns a string that specifies the name of the L&F class
232 * that renders this component.
233 *
234 * @return the string "CheckBoxUI"
235 * @see JComponent#getUIClassID
236 * @see UIDefaults#getUI
237 * @beaninfo
238 * expert: true
239 * description: A string that specifies the name of the L&F class
240 */
241 public String getUIClassID() {
242 return uiClassID;
243 }
244
245 /**
246 * The icon for checkboxs comes from the look and feel,
247 * not the Action; this is overriden to do nothing.
248 */
249 void setIconFromAction(Action a) {
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 /**
268 * See JComponent.readObject() for information about serialization
269 * in Swing.
270 */
271 private void readObject(ObjectInputStream s) throws IOException,
272 ClassNotFoundException {
273 s.defaultReadObject();
274 if (getUIClassID().equals(uiClassID)) {
275 updateUI();
276 }
277 }
278
279 /**
280 * Returns a string representation of this JCheckBox. This method
281 * is intended to be used only for debugging purposes, and the
282 * content and format of the returned string may vary between
283 * implementations. The returned string may be empty but may not
284 * be <code>null</code>.
285 * specific new aspects of the JFC components.
286 *
287 * @return a string representation of this JCheckBox.
288 */
289 protected String paramString() {
290 return super .paramString();
291 }
292
293 /////////////////
294 // Accessibility support
295 ////////////////
296
297 /**
298 * Gets the AccessibleContext associated with this JCheckBox.
299 * For JCheckBoxes, the AccessibleContext takes the form of an
300 * AccessibleJCheckBox.
301 * A new AccessibleJCheckBox instance is created if necessary.
302 *
303 * @return an AccessibleJCheckBox that serves as the
304 * AccessibleContext of this JCheckBox
305 * @beaninfo
306 * expert: true
307 * description: The AccessibleContext associated with this CheckBox.
308 */
309 public AccessibleContext getAccessibleContext() {
310 if (accessibleContext == null) {
311 accessibleContext = new AccessibleJCheckBox();
312 }
313 return accessibleContext;
314 }
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 /**
334 * Get the role of this object.
335 *
336 * @return an instance of AccessibleRole describing the role of the object
337 * @see AccessibleRole
338 */
339 public AccessibleRole getAccessibleRole() {
340 return AccessibleRole.CHECK_BOX;
341 }
342
343 } // inner class AccessibleJCheckBox
344 }
|