KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JCheckBox


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
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.beans.*;
12
13 import javax.swing.plaf.*;
14 import javax.accessibility.*;
15
16 import java.io.ObjectOutputStream JavaDoc;
17 import java.io.ObjectInputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19
20
21 /**
22  * An implementation of a check box -- an item that can be selected or
23  * deselected, and which displays its state to the user.
24  * By convention, any number of check boxes in a group can be selected.
25  * See <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
26  * in <em>The Java Tutorial</em>
27  * for examples and information on using check boxes.
28  * <p>
29  * <strong>Warning:</strong>
30  * Serialized objects of this class will not be compatible with
31  * future Swing releases. The current serialization support is
32  * appropriate for short term storage or RMI between applications running
33  * the same version of Swing. As of 1.4, support for long term storage
34  * of all JavaBeans<sup><font size="-2">TM</font></sup>
35  * has been added to the <code>java.beans</code> package.
36  * Please see {@link java.beans.XMLEncoder}.
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.73 12/19/03
45  * @author Jeff Dinkins
46  */

47 public class JCheckBox extends JToggleButton JavaDoc implements Accessible {
48
49     /** Identifies a change to the flat property. */
50     public static final String JavaDoc BORDER_PAINTED_FLAT_CHANGED_PROPERTY = "borderPaintedFlat";
51
52     private boolean flat = false;
53
54     /**
55      * @see #getUIClassID
56      * @see #readObject
57      */

58     private static final String JavaDoc uiClassID = "CheckBoxUI";
59
60
61     /**
62      * Creates an initially unselected check box button with no text, no icon.
63      */

64     public JCheckBox () {
65         this(null, null, false);
66     }
67
68     /**
69      * Creates an initially unselected check box with an icon.
70      *
71      * @param icon the Icon image to display
72      */

73     public JCheckBox(Icon JavaDoc icon) {
74         this(null, icon, false);
75     }
76     
77     /**
78      * Creates a check box with an icon and specifies whether
79      * or not it is initially selected.
80      *
81      * @param icon the Icon image to display
82      * @param selected a boolean value indicating the initial selection
83      * state. If <code>true</code> the check box is selected
84      */

85     public JCheckBox(Icon JavaDoc icon, boolean selected) {
86         this(null, icon, selected);
87     }
88     
89     /**
90      * Creates an initially unselected check box with text.
91      *
92      * @param text the text of the check box.
93      */

94     public JCheckBox (String JavaDoc text) {
95         this(text, null, false);
96     }
97
98     /**
99      * Creates a check box where properties are taken from the
100      * Action supplied.
101      *
102      * @since 1.3
103      */

104     public JCheckBox(Action JavaDoc a) {
105         this();
106     setAction(a);
107     }
108
109
110     /**
111      * Creates a check box with text and specifies whether
112      * or not it is initially selected.
113      *
114      * @param text the text of the check box.
115      * @param selected a boolean value indicating the initial selection
116      * state. If <code>true</code> the check box is selected
117      */

118     public JCheckBox (String JavaDoc text, boolean selected) {
119         this(text, null, selected);
120     }
121
122     /**
123      * Creates an initially unselected check box with
124      * the specified text and icon.
125      *
126      * @param text the text of the check box.
127      * @param icon the Icon image to display
128      */

129     public JCheckBox(String JavaDoc text, Icon JavaDoc icon) {
130         this(text, icon, false);
131     }
132
133     /**
134      * Creates a check box with text and icon,
135      * and specifies whether or not it is initially selected.
136      *
137      * @param text the text of the check box.
138      * @param icon the Icon image to display
139      * @param selected a boolean value indicating the initial selection
140      * state. If <code>true</code> the check box is selected
141      */

142     public JCheckBox (String JavaDoc text, Icon JavaDoc icon, boolean selected) {
143         super(text, icon, selected);
144         setUIProperty("borderPainted", Boolean.FALSE);
145         setHorizontalAlignment(LEADING);
146     }
147
148     /**
149      * Sets the <code>borderPaintedFlat</code> property,
150      * which gives a hint to the look and feel as to the
151      * appearance of the check box border.
152      * This is usually set to <code>true</code> when a
153      * <code>JCheckBox</code> instance is used as a
154      * renderer in a component such as a <code>JTable</code> or
155      * <code>JTree</code>. The default value for the
156      * <code>borderPaintedFlat</code> property is <code>false</code>.
157      * This method fires a property changed event.
158      * Some look and feels might not implement flat borders;
159      * they will ignore this property.
160      *
161      * @param b <code>true</code> requests that the border be painted flat;
162      * <code>false</code> requests normal borders
163      * @see #isBorderPaintedFlat
164      * @beaninfo
165      * bound: true
166      * attribute: visualUpdate true
167      * description: Whether the border is painted flat.
168      */

169     public void setBorderPaintedFlat(boolean b) {
170         boolean oldValue = flat;
171         flat = b;
172         firePropertyChange(BORDER_PAINTED_FLAT_CHANGED_PROPERTY, oldValue, flat);
173         if (b != oldValue) {
174             revalidate();
175             repaint();
176     }
177     }
178
179     /**
180      * Gets the value of the <code>borderPaintedFlat</code> property.
181      *
182      * @return the value of the <code>borderPaintedFlat</code> property
183      * @see #setBorderPaintedFlat
184      */

185     public boolean isBorderPaintedFlat() {
186     return flat;
187     }
188
189     /**
190      * Resets the UI property to a value from the current look and feel.
191      *
192      * @see JComponent#updateUI
193      */

194     public void updateUI() {
195         setUI((ButtonUI)UIManager.getUI(this));
196     }
197
198
199     /**
200      * Returns a string that specifies the name of the L&F class
201      * that renders this component.
202      *
203      * @return the string "CheckBoxUI"
204      * @see JComponent#getUIClassID
205      * @see UIDefaults#getUI
206      * @beaninfo
207      * expert: true
208      * description: A string that specifies the name of the L&F class
209      */

210     public String JavaDoc getUIClassID() {
211         return uiClassID;
212     }
213
214
215     /**
216      * Factory method which sets the <code>ActionEvent</code> source's
217      * properties according to values from the Action instance. The
218      * properties which are set may differ for subclasses.
219      * By default, the properties which get set are <code>Text, Mnemonic,
220      * Enabled, ActionCommand</code>, and <code>ToolTipText</code>.
221      *
222      * @param a the Action from which to get the properties, or null
223      * @since 1.3
224      * @see Action
225      * @see #setAction
226      */

227     protected void configurePropertiesFromAction(Action JavaDoc a) {
228         String JavaDoc[] types = { Action.MNEMONIC_KEY, Action.NAME,
229                            Action.SHORT_DESCRIPTION,
230                            Action.ACTION_COMMAND_KEY, "enabled" };
231         configurePropertiesFromAction(a, types);
232     }
233
234     /**
235      * Factory method which creates the PropertyChangeListener
236      * used to update the ActionEvent source as properties change on
237      * its Action instance. Subclasses may override this in order
238      * to provide their own PropertyChangeListener if the set of
239      * properties which should be kept up to date differs from the
240      * default properties (Text, Icon, Enabled, ToolTipText).
241      *
242      * Note that PropertyChangeListeners should avoid holding
243      * strong references to the ActionEvent source, as this may hinder
244      * garbage collection of the ActionEvent source and all components
245      * in its containment hierarchy.
246      *
247      * @since 1.3
248      * @see Action
249      * @see #setAction
250      */

251     protected PropertyChangeListener createActionPropertyChangeListener(Action JavaDoc a) {
252         return new AbstractActionPropertyChangeListener JavaDoc(this, a) {
253         public void propertyChange(PropertyChangeEvent e) {
254         String JavaDoc propertyName = e.getPropertyName();
255         AbstractButton JavaDoc button = (AbstractButton JavaDoc)getTarget();
256         if (button == null) { //WeakRef GC'ed in 1.2
257
Action JavaDoc action = (Action JavaDoc)e.getSource();
258             action.removePropertyChangeListener(this);
259         } else {
260             if (propertyName.equals(Action.NAME)) {
261             String JavaDoc text = (String JavaDoc) e.getNewValue();
262             button.setText(text);
263             button.repaint();
264             } else if (propertyName.equals(Action.SHORT_DESCRIPTION)) {
265             String JavaDoc text = (String JavaDoc) e.getNewValue();
266             button.setToolTipText(text);
267             } else if (propertyName.equals("enabled")) {
268             Boolean JavaDoc enabledState = (Boolean JavaDoc) e.getNewValue();
269             button.setEnabled(enabledState.booleanValue());
270             button.repaint();
271             } else if (propertyName.equals(Action.ACTION_COMMAND_KEY)) {
272                         button.setActionCommand((String JavaDoc)e.getNewValue());
273             }
274         }
275         }
276     };
277     }
278
279
280      /*
281       * See readObject and writeObject in JComponent for more
282       * information about serialization in Swing.
283       */

284      private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
285         s.defaultWriteObject();
286         if (getUIClassID().equals(uiClassID)) {
287             byte count = JComponent.getWriteObjCounter(this);
288             JComponent.setWriteObjCounter(this, --count);
289             if (count == 0 && ui != null) {
290                 ui.installUI(this);
291             }
292         }
293      }
294
295
296     /**
297      * See JComponent.readObject() for information about serialization
298      * in Swing.
299      */

300     private void readObject(ObjectInputStream JavaDoc s)
301     throws IOException JavaDoc, ClassNotFoundException JavaDoc
302     {
303         s.defaultReadObject();
304     if (getUIClassID().equals(uiClassID)) {
305         updateUI();
306     }
307     }
308
309
310     /**
311      * Returns a string representation of this JCheckBox. This method
312      * is intended to be used only for debugging purposes, and the
313      * content and format of the returned string may vary between
314      * implementations. The returned string may be empty but may not
315      * be <code>null</code>.
316      * specific new aspects of the JFC components.
317      *
318      * @return a string representation of this JCheckBox.
319      */

320     protected String JavaDoc paramString() {
321     return super.paramString();
322     }
323
324 /////////////////
325
// Accessibility support
326
////////////////
327

328     /**
329      * Gets the AccessibleContext associated with this JCheckBox.
330      * For JCheckBoxes, the AccessibleContext takes the form of an
331      * AccessibleJCheckBox.
332      * A new AccessibleJCheckBox instance is created if necessary.
333      *
334      * @return an AccessibleJCheckBox that serves as the
335      * AccessibleContext of this JCheckBox
336      * @beaninfo
337      * expert: true
338      * description: The AccessibleContext associated with this CheckBox.
339      */

340     public AccessibleContext getAccessibleContext() {
341         if (accessibleContext == null) {
342             accessibleContext = new AccessibleJCheckBox();
343         }
344         return accessibleContext;
345     }
346
347     /**
348      * This class implements accessibility support for the
349      * <code>JCheckBox</code> class. It provides an implementation of the
350      * Java Accessibility API appropriate to check box user-interface
351      * elements.
352      * <p>
353      * <strong>Warning:</strong>
354      * Serialized objects of this class will not be compatible with
355      * future Swing releases. The current serialization support is
356      * appropriate for short term storage or RMI between applications running
357      * the same version of Swing. As of 1.4, support for long term storage
358      * of all JavaBeans<sup><font size="-2">TM</font></sup>
359      * has been added to the <code>java.beans</code> package.
360      * Please see {@link java.beans.XMLEncoder}.
361      */

362     protected class AccessibleJCheckBox extends AccessibleJToggleButton {
363
364         /**
365          * Get the role of this object.
366          *
367          * @return an instance of AccessibleRole describing the role of the object
368          * @see AccessibleRole
369          */

370         public AccessibleRole getAccessibleRole() {
371             return AccessibleRole.CHECK_BOX;
372         }
373
374     } // inner class AccessibleJCheckBox
375
}
376   
377
Popular Tags