KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > CheckboxGroup


1 /*
2  * @(#)CheckboxGroup.java 1.36 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 /**
10  * The <code>CheckboxGroup</code> class is used to group together
11  * a set of <code>Checkbox</code> buttons.
12  * <p>
13  * Exactly one check box button in a <code>CheckboxGroup</code> can
14  * be in the "on" state at any given time. Pushing any
15  * button sets its state to "on" and forces any other button that
16  * is in the "on" state into the "off" state.
17  * <p>
18  * The following code example produces a new check box group,
19  * with three check boxes:
20  * <p>
21  * <hr><blockquote><pre>
22  * setLayout(new GridLayout(3, 1));
23  * CheckboxGroup cbg = new CheckboxGroup();
24  * add(new Checkbox("one", cbg, true));
25  * add(new Checkbox("two", cbg, false));
26  * add(new Checkbox("three", cbg, false));
27  * </pre></blockquote><hr>
28  * <p>
29  * This image depicts the check box group created by this example:
30  * <p>
31  * <img SRC="doc-files/CheckboxGroup-1.gif"
32  * alt="Shows three checkboxes, arranged vertically, labeled one, two, and three. Checkbox one is in the on state."
33  * ALIGN=center HSPACE=10 VSPACE=7>
34  * <p>
35  * @version 1.36 05/18/04
36  * @author Sami Shaio
37  * @see java.awt.Checkbox
38  * @since JDK1.0
39  */

40 public class CheckboxGroup implements java.io.Serializable JavaDoc {
41     /**
42      * The current choice.
43      * @serial
44      * @see #getCurrent()
45      * @see #setCurrent(Checkbox)
46      */

47     Checkbox JavaDoc selectedCheckbox = null;
48
49     /*
50      * JDK 1.1 serialVersionUID
51      */

52     private static final long serialVersionUID = 3729780091441768983L;
53
54     /**
55      * Creates a new instance of <code>CheckboxGroup</code>.
56      */

57     public CheckboxGroup() {
58     }
59
60     /**
61      * Gets the current choice from this check box group.
62      * The current choice is the check box in this
63      * group that is currently in the "on" state,
64      * or <code>null</code> if all check boxes in the
65      * group are off.
66      * @return the check box that is currently in the
67      * "on" state, or <code>null</code>.
68      * @see java.awt.Checkbox
69      * @see java.awt.CheckboxGroup#setSelectedCheckbox
70      * @since JDK1.1
71      */

72     public Checkbox JavaDoc getSelectedCheckbox() {
73     return getCurrent();
74     }
75
76     /**
77      * @deprecated As of JDK version 1.1,
78      * replaced by <code>getSelectedCheckbox()</code>.
79      */

80     @Deprecated JavaDoc
81     public Checkbox JavaDoc getCurrent() {
82     return selectedCheckbox;
83     }
84
85     /**
86      * Sets the currently selected check box in this group
87      * to be the specified check box.
88      * This method sets the state of that check box to "on" and
89      * sets all other check boxes in the group to be off.
90      * <p>
91      * If the check box argument is <tt>null</tt>, all check boxes
92      * in this check box group are deselected. If the check box argument
93      * belongs to a different check box group, this method does
94      * nothing.
95      * @param box the <code>Checkbox</code> to set as the
96      * current selection.
97      * @see java.awt.Checkbox
98      * @see java.awt.CheckboxGroup#getSelectedCheckbox
99      * @since JDK1.1
100      */

101     public void setSelectedCheckbox(Checkbox JavaDoc box) {
102         setCurrent(box);
103     }
104
105     /**
106      * @deprecated As of JDK version 1.1,
107      * replaced by <code>setSelectedCheckbox(Checkbox)</code>.
108      */

109     @Deprecated JavaDoc
110     public synchronized void setCurrent(Checkbox JavaDoc box) {
111     if (box != null && box.group != this) {
112         return;
113     }
114     Checkbox JavaDoc oldChoice = this.selectedCheckbox;
115     this.selectedCheckbox = box;
116     if (oldChoice != null && oldChoice != box && oldChoice.group == this) {
117         oldChoice.setState(false);
118     }
119     if (box != null && oldChoice != box && !box.getState()) {
120         box.setStateInternal(true);
121     }
122     }
123
124     /**
125      * Returns a string representation of this check box group,
126      * including the value of its current selection.
127      * @return a string representation of this check box group.
128      */

129     public String JavaDoc toString() {
130     return getClass().getName() + "[selectedCheckbox=" + selectedCheckbox + "]";
131     }
132
133 }
134
Popular Tags