2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
package javax.swing;
|
|
26 |
|
|
27 |
import java.awt.*;
|
|
28 |
import java.awt.event.*;
|
|
29 |
import java.beans.*;
|
|
30 |
|
|
31 |
import javax.swing.plaf.*;
|
|
32 |
import javax.accessibility.*;
|
|
33 |
|
|
34 |
import java.io.ObjectOutputStream;
|
|
35 |
import java.io.ObjectInputStream;
|
|
36 |
import java.io.IOException;
|
|
37 |
|
|
38 |
|
|
39 |
/**
|
|
40 |
* An implementation of a check box -- an item that can be selected or
|
|
41 |
* deselected, and which displays its state to the user.
|
|
42 |
* By convention, any number of check boxes in a group can be selected.
|
|
43 |
* See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
|
|
44 |
* in <em>The Java Tutorial</em>
|
|
45 |
* for examples and information on using check boxes.
|
|
46 |
* <p>
|
|
47 |
* Buttons can be configured, and to some degree controlled, by
|
|
48 |
* <code><a href="Action.html">Action</a></code>s. Using an
|
|
49 |
* <code>Action</code> with a button has many benefits beyond directly
|
|
50 |
* configuring a button. Refer to <a href="Action.html#buttonActions">
|
|
51 |
* Swing Components Supporting <code>Action</code></a> for more
|
|
52 |
* details, and you can find more information in <a
|
|
53 |
* href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
|
|
54 |
* to Use Actions</a>, a section in <em>The Java Tutorial</em>.
|
|
55 |
* <p>
|
|
56 |
* <strong>Warning:</strong> Swing is not thread safe. For more
|
|
57 |
* information see <a
|
|
58 |
* href="package-summary.html#threading">Swing's Threading
|
|
59 |
* Policy</a>.
|
|
60 |
* <p>
|
|
61 |
* <strong>Warning:</strong>
|
|
62 |
* Serialized objects of this class will not be compatible with
|
|
63 |
* future Swing releases. The current serialization support is
|
|
64 |
* appropriate for short term storage or RMI between applications running
|
|
65 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
66 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
67 |
* has been added to the <code>java.beans</code> package.
|
|
68 |
* Please see {@link java.beans.XMLEncoder}.
|
|
69 |
*
|
|
70 |
* @see JRadioButton
|
|
71 |
*
|
|
72 |
* @beaninfo
|
|
73 |
* attribute: isContainer false
|
|
74 |
* description: A component which can be selected or deselected.
|
|
75 |
*
|
|
76 |
* @author Jeff Dinkins
|
|
77 |
*/
|
|
78 |
public class JCheckBox extends JToggleButton implements Accessible {
|
|
79 |
|
|
80 |
/** Identifies a change to the flat property. */
|
|
81 |
public static final String BORDER_PAINTED_FLAT_CHANGED_PROPERTY = "borderPaintedFlat";
|
|
82 |
|
|
83 |
private boolean flat = false;
|
|
84 |
|
|
85 |
/**
|
|
86 |
* @see #getUIClassID
|
|
87 |
* @see #readObject
|
|
88 |
*/
|
|
89 |
private static final String uiClassID = "CheckBoxUI";
|
|
90 |
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Creates an initially unselected check box button with no text, no icon.
|
|
94 |
*/
|
|
95 |
public JCheckBox () {
|
|
96 |
this(null, null, false);
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Creates an initially unselected check box with an icon.
|
|
101 |
*
|
|
102 |
* @param icon the Icon image to display
|
|
103 |
*/
|
|
104 |
public JCheckBox(Icon icon) {
|
|
105 |
this(null, icon, false);
|
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* Creates a check box with an icon and specifies whether
|
|
110 |
* or not it is initially selected.
|
|
111 |
*
|
|
112 |
* @param icon the Icon image to display
|
|
113 |
* @param selected a boolean value indicating the initial selection
|
|
114 |
* state. If <code>true</code> the check box is selected
|
|
115 |
*/
|
|
116 |
public JCheckBox(Icon icon, boolean selected) {
|
|
117 |
this(null, icon, selected);
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Creates an initially unselected check box with text.
|
|
122 |
*
|
|
123 |
* @param text the text of the check box.
|
|
124 |
*/
|
|
125 |
public JCheckBox (String text) {
|
|
126 |
this(text, null, false);
|
|
127 |
}
|
|
128 |
|
|
129 |
/**
|
|
130 |
* Creates a check box where properties are taken from the
|
|
131 |
* Action supplied.
|
|
132 |
*
|
|
133 |
* @since 1.3
|
|
134 |
*/
|
|
135 |
public JCheckBox(Action a) {
|
|
136 |
this();
|
|
137 |
setAction(a);
|
|
138 |
}
|
|
139 |
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Creates a check box with text and specifies whether
|
|
143 |
* or not it is initially selected.
|
|
144 |
*
|
|
145 |
* @param text the text of the check box.
|
|
146 |
* @param selected a boolean value indicating the initial selection
|
|
147 |
* state. If <code>true</code> the check box is selected
|
|
148 |
*/
|
|
149 |
public JCheckBox (String text, boolean selected) {
|
|
150 |
this(text, null, selected);
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Creates an initially unselected check box with
|
|
155 |
* the specified text and icon.
|
|
156 |
*
|
|
157 |
* @param text the text of the check box.
|
|
158 |
* @param icon the Icon image to display
|
|
159 |
*/
|
|
160 |
public JCheckBox(String text, Icon icon) {
|
|
161 |
this(text, icon, false);
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* Creates a check box with text and icon,
|
|
166 |
* and specifies whether or not it is initially selected.
|
|
167 |
*
|
|
168 |
* @param text the text of the check box.
|
|
169 |
* @param icon the Icon image to display
|
|
170 |
* @param selected a boolean value indicating the initial selection
|
|
171 |
* state. If <code>true</code> the check box is selected
|
|
172 |
*/
|
|
173 |
public JCheckBox (String text, Icon icon, boolean selected) {
|
|
174 |
super(text, icon, selected);
|
|
175 |
setUIProperty("borderPainted", Boolean.FALSE);
|
|
176 |
setHorizontalAlignment(LEADING);
|
|
177 |
}
|
|
178 |
|
|
179 |
/**
|
|
180 |
* Sets the <code>borderPaintedFlat</code> property,
|
|
181 |
* which gives a hint to the look and feel as to the
|
|
182 |
* appearance of the check box border.
|
|
183 |
* This is usually set to <code>true</code> when a
|
|
184 |
* <code>JCheckBox</code> instance is used as a
|
|
185 |
* renderer in a component such as a <code>JTable</code> or
|
|
186 |
* <code>JTree</code>. The default value for the
|
|
187 |
* <code>borderPaintedFlat</code> property is <code>false</code>.
|
|
188 |
* This method fires a property changed event.
|
|
189 |
* Some look and feels might not implement flat borders;
|
|
190 |
* they will ignore this property.
|
|
191 |
*
|
|
192 |
* @param b <code>true</code> requests that the border be painted flat;
|
|
193 |
* <code>false</code> requests normal borders
|
|
194 |
* @see #isBorderPaintedFlat
|
|
195 |
* @beaninfo
|
|
196 |
* bound: true
|
|
197 |
* attribute: visualUpdate true
|
|
198 |
* description: Whether the border is painted flat.
|
|
199 |
* @since 1.3
|
|
200 |
*/
|
|
201 |
public void setBorderPaintedFlat(boolean b) {
|
|
202 |
boolean oldValue = flat;
|
|
203 |
flat = b;
|
|
204 |
firePropertyChange(BORDER_PAINTED_FLAT_CHANGED_PROPERTY, oldValue, flat);
|
|
205 |
if (b != oldValue) {
|
|
206 |
revalidate();
|
|
207 |
repaint();
|
|
208 |
}
|
|
209 |
}
|
|
210 |
|
|
211 |
/**
|
|
212 |
* Gets the value of the <code>borderPaintedFlat</code> property.
|
|
213 |
*
|
|
214 |
* @return the value of the <code>borderPaintedFlat</code> property
|
|
215 |
* @see #setBorderPaintedFlat
|
|
216 |
* @since 1.3
|
|
217 |
*/
|
|
218 |
public boolean isBorderPaintedFlat() {
|
|
219 |
return flat;
|
|
220 |
}
|
|
221 |
|
|
222 |
/**
|
|
223 |
* Resets the UI property to a value from the current look and feel.
|
|
224 |
*
|
|
225 |
* @see JComponent#updateUI
|
|
226 |
*/
|
|
227 |
public void updateUI() {
|
|
228 |
setUI((ButtonUI)UIManager.getUI(this));
|
|
229 |
}
|
|
230 |
|
|
231 |
|
|
232 |
/**
|
|
233 |
* Returns a string that specifies the name of the L&F class
|
|
234 |
* that renders this component.
|
|
235 |
*
|
|
236 |
* @return the string "CheckBoxUI"
|
|
237 |
* @see JComponent#getUIClassID
|
|
238 |
* @see UIDefaults#getUI
|
|
239 |
* @beaninfo
|
|
240 |
* expert: true
|
|
241 |
* description: A string that specifies the name of the L&F class
|
|
242 |
*/
|
|
243 |
public String getUIClassID() {
|
|
244 |
return uiClassID;
|
|
245 |
}
|
|
246 |
|
|
247 |
|
|
248 |
/**
|
|
249 |
* The icon for checkboxs comes from the look and feel,
|
|
250 |
* not the Action; this is overriden to do nothing.
|
|
251 |
*/
|
|
252 |
void setIconFromAction(Action a) {
|
|
253 |
}
|
|
254 |
|
|
255 |
/*
|
|
256 |
* See readObject and writeObject in JComponent for more
|
|
257 |
* information about serialization in Swing.
|
|
258 |
*/
|
|
259 |
private void writeObject(ObjectOutputStream s) throws IOException {
|
|
260 |
s.defaultWriteObject();
|
|
261 |
if (getUIClassID().equals(uiClassID)) {
|
|
262 |
byte count = JComponent.getWriteObjCounter(this);
|
|
263 |
JComponent.setWriteObjCounter(this, --count);
|
|
264 |
if (count == 0 && ui != null) {
|
|
265 |
ui.installUI(this);
|
|
266 |
}
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
|
|
271 |
/**
|
|
272 |
* See JComponent.readObject() for information about serialization
|
|
273 |
* in Swing.
|
|
274 |
*/
|
|
275 |
private void readObject(ObjectInputStream s)
|
|
276 |
throws IOException, ClassNotFoundException
|
|
277 |
{
|
|
278 |
s.defaultReadObject();
|
|
279 |
if (getUIClassID().equals(uiClassID)) {
|
|
280 |
updateUI();
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
|
284 |
|
|
285 |
/**
|
|
286 |
* Returns a string representation of this JCheckBox. This method
|
|
287 |
* is intended to be used only for debugging purposes, and the
|
|
288 |
* content and format of the returned string may vary between
|
|
289 |
* implementations. The returned string may be empty but may not
|
|
290 |
* be <code>null</code>.
|
|
291 |
* specific new aspects of the JFC components.
|
|
292 |
*
|
|
293 |
* @return a string representation of this JCheckBox.
|
|
294 |
*/
|
|
295 |
protected String paramString() {
|
|
296 |
return super.paramString();
|
|
297 |
}
|
|
298 |
|
|
299 |
/////////////////
|
|
300 |
// Accessibility support
|
|
301 |
////////////////
|
|
302 |
|
|
303 |
/**
|
|
304 |
* Gets the AccessibleContext associated with this JCheckBox.
|
|
305 |
* For JCheckBoxes, the AccessibleContext takes the form of an
|
|
306 |
* AccessibleJCheckBox.
|
|
307 |
* A new AccessibleJCheckBox instance is created if necessary.
|
|
308 |
*
|
|
309 |
* @return an AccessibleJCheckBox that serves as the
|
|
310 |
* AccessibleContext of this JCheckBox
|
|
311 |
* @beaninfo
|
|
312 |
* expert: true
|
|
313 |
* description: The AccessibleContext associated with this CheckBox.
|
|
314 |
*/
|
|
315 |
public AccessibleContext getAccessibleContext() {
|
|
316 |
if (accessibleContext == null) {
|
|
317 |
accessibleContext = new AccessibleJCheckBox();
|
|
318 |
}
|
|
319 |
return accessibleContext;
|
|
320 |
}
|
|
321 |
|
|
322 |
/**
|
|
323 |
* This class implements accessibility support for the
|
|
324 |
* <code>JCheckBox</code> class. It provides an implementation of the
|
|
325 |
* Java Accessibility API appropriate to check box user-interface
|
|
326 |
* elements.
|
|
327 |
* <p>
|
|
328 |
* <strong>Warning:</strong>
|
|
329 |
* Serialized objects of this class will not be compatible with
|
|
330 |
* future Swing releases. The current serialization support is
|
|
331 |
* appropriate for short term storage or RMI between applications running
|
|
332 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
333 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
334 |
* has been added to the <code>java.beans</code> package.
|
|
335 |
* Please see {@link java.beans.XMLEncoder}.
|
|
336 |
*/
|
|
337 |
protected class AccessibleJCheckBox extends AccessibleJToggleButton {
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Get the role of this object.
|
|
341 |
*
|
|
342 |
* @return an instance of AccessibleRole describing the role of the object
|
|
343 |
* @see AccessibleRole
|
|
344 |
*/
|
|
345 |
public AccessibleRole getAccessibleRole() {
|
|
346 |
return AccessibleRole.CHECK_BOX;
|
|
347 |
}
|
|
348 |
|
|
349 |
} // inner class AccessibleJCheckBox
|
|
350 |
}
|