1 /*
2 * Copyright 1995-2004 Sun Microsystems, Inc. All Rights Reserved.
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
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
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 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 package java.awt;
26
27 /**
28 * The <code>CheckboxGroup</code> class is used to group together
29 * a set of <code>Checkbox</code> buttons.
30 * <p>
31 * Exactly one check box button in a <code>CheckboxGroup</code> can
32 * be in the "on" state at any given time. Pushing any
33 * button sets its state to "on" and forces any other button that
34 * is in the "on" state into the "off" state.
35 * <p>
36 * The following code example produces a new check box group,
37 * with three check boxes:
38 * <p>
39 * <hr><blockquote><pre>
40 * setLayout(new GridLayout(3, 1));
41 * CheckboxGroup cbg = new CheckboxGroup();
42 * add(new Checkbox("one", cbg, true));
43 * add(new Checkbox("two", cbg, false));
44 * add(new Checkbox("three", cbg, false));
45 * </pre></blockquote><hr>
46 * <p>
47 * This image depicts the check box group created by this example:
48 * <p>
49 * <img src="doc-files/CheckboxGroup-1.gif"
50 * alt="Shows three checkboxes, arranged vertically, labeled one, two, and three. Checkbox one is in the on state."
51 * ALIGN=center HSPACE=10 VSPACE=7>
52 * <p>
53 * @author Sami Shaio
54 * @see java.awt.Checkbox
55 * @since JDK1.0
56 */
57 public class CheckboxGroup implements java.io.Serializable {
58 /**
59 * The current choice.
60 * @serial
61 * @see #getCurrent()
62 * @see #setCurrent(Checkbox)
63 */
64 Checkbox selectedCheckbox = null;
65
66 /*
67 * JDK 1.1 serialVersionUID
68 */
69 private static final long serialVersionUID = 3729780091441768983L;
70
71 /**
72 * Creates a new instance of <code>CheckboxGroup</code>.
73 */
74 public CheckboxGroup() {
75 }
76
77 /**
78 * Gets the current choice from this check box group.
79 * The current choice is the check box in this
80 * group that is currently in the "on" state,
81 * or <code>null</code> if all check boxes in the
82 * group are off.
83 * @return the check box that is currently in the
84 * "on" state, or <code>null</code>.
85 * @see java.awt.Checkbox
86 * @see java.awt.CheckboxGroup#setSelectedCheckbox
87 * @since JDK1.1
88 */
89 public Checkbox getSelectedCheckbox() {
90 return getCurrent();
91 }
92
93 /**
94 * @deprecated As of JDK version 1.1,
95 * replaced by <code>getSelectedCheckbox()</code>.
96 */
97 @Deprecated
98 public Checkbox getCurrent() {
99 return selectedCheckbox;
100 }
101
102 /**
103 * Sets the currently selected check box in this group
104 * to be the specified check box.
105 * This method sets the state of that check box to "on" and
106 * sets all other check boxes in the group to be off.
107 * <p>
108 * If the check box argument is <tt>null</tt>, all check boxes
109 * in this check box group are deselected. If the check box argument
110 * belongs to a different check box group, this method does
111 * nothing.
112 * @param box the <code>Checkbox</code> to set as the
113 * current selection.
114 * @see java.awt.Checkbox
115 * @see java.awt.CheckboxGroup#getSelectedCheckbox
116 * @since JDK1.1
117 */
118 public void setSelectedCheckbox(Checkbox box) {
119 setCurrent(box);
120 }
121
122 /**
123 * @deprecated As of JDK version 1.1,
124 * replaced by <code>setSelectedCheckbox(Checkbox)</code>.
125 */
126 @Deprecated
127 public synchronized void setCurrent(Checkbox box) {
128 if (box != null && box.group != this) {
129 return;
130 }
131 Checkbox oldChoice = this.selectedCheckbox;
132 this.selectedCheckbox = box;
133 if (oldChoice != null && oldChoice != box && oldChoice.group == this) {
134 oldChoice.setState(false);
135 }
136 if (box != null && oldChoice != box && !box.getState()) {
137 box.setStateInternal(true);
138 }
139 }
140
141 /**
142 * Returns a string representation of this check box group,
143 * including the value of its current selection.
144 * @return a string representation of this check box group.
145 */
146 public String toString() {
147 return getClass().getName() + "[selectedCheckbox=" + selectedCheckbox + "]";
148 }
149
150 }