Path: blob/master/src/java.desktop/share/classes/java/awt/CheckboxGroup.java
41152 views
/*1* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.io.Serial;2829/**30* The {@code CheckboxGroup} class is used to group together31* a set of {@code Checkbox} buttons.32* <p>33* Exactly one check box button in a {@code CheckboxGroup} can34* be in the "on" state at any given time. Pushing any35* button sets its state to "on" and forces any other button that36* is in the "on" state into the "off" state.37* <p>38* The following code example produces a new check box group,39* with three check boxes:40*41* <hr><blockquote><pre>42* setLayout(new GridLayout(3, 1));43* CheckboxGroup cbg = new CheckboxGroup();44* add(new Checkbox("one", cbg, true));45* add(new Checkbox("two", cbg, false));46* add(new Checkbox("three", cbg, false));47* </pre></blockquote><hr>48* <p>49* This image depicts the check box group created by this example:50* <p>51* <img src="doc-files/CheckboxGroup-1.gif" alt="Shows three checkboxes,52* arranged vertically, labeled one, two, and three. Checkbox one is in the on53* state." style="margin: 7px 10px;">54*55* @author Sami Shaio56* @see java.awt.Checkbox57* @since 1.058*/59public class CheckboxGroup implements java.io.Serializable {60/**61* The current choice.62* @serial63* @see #getCurrent()64* @see #setCurrent(Checkbox)65*/66Checkbox selectedCheckbox = null;6768/**69* Use serialVersionUID from JDK 1.1 for interoperability.70*/71@Serial72private static final long serialVersionUID = 3729780091441768983L;7374/**75* Creates a new instance of {@code CheckboxGroup}.76*/77public CheckboxGroup() {78}7980/**81* Gets the current choice from this check box group.82* The current choice is the check box in this83* group that is currently in the "on" state,84* or {@code null} if all check boxes in the85* group are off.86* @return the check box that is currently in the87* "on" state, or {@code null}.88* @see java.awt.Checkbox89* @see java.awt.CheckboxGroup#setSelectedCheckbox90* @since 1.191*/92public Checkbox getSelectedCheckbox() {93return getCurrent();94}9596/**97* Returns the current choice from this check box group98* or {@code null} if none of checkboxes are selected.99*100* @return the selected checkbox101* @deprecated As of JDK version 1.1,102* replaced by {@code getSelectedCheckbox()}.103*/104@Deprecated105public Checkbox getCurrent() {106return selectedCheckbox;107}108109/**110* Sets the currently selected check box in this group111* to be the specified check box.112* This method sets the state of that check box to "on" and113* sets all other check boxes in the group to be off.114* <p>115* If the check box argument is {@code null}, all check boxes116* in this check box group are deselected. If the check box argument117* belongs to a different check box group, this method does118* nothing.119* @param box the {@code Checkbox} to set as the120* current selection.121* @see java.awt.Checkbox122* @see java.awt.CheckboxGroup#getSelectedCheckbox123* @since 1.1124*/125public void setSelectedCheckbox(Checkbox box) {126setCurrent(box);127}128129/**130* Sets the currently selected check box in this group131* to be the specified check box and unsets all others.132*133* @param box the {@code Checkbox} to set as the134* current selection.135* @deprecated As of JDK version 1.1,136* replaced by {@code setSelectedCheckbox(Checkbox)}.137*/138@Deprecated139public synchronized void setCurrent(Checkbox box) {140if (box != null && box.group != this) {141return;142}143Checkbox oldChoice = this.selectedCheckbox;144this.selectedCheckbox = box;145if (oldChoice != null && oldChoice != box && oldChoice.group == this) {146oldChoice.setState(false);147}148if (box != null && oldChoice != box && !box.getState()) {149box.setStateInternal(true);150}151}152153/**154* Returns a string representation of this check box group,155* including the value of its current selection.156* @return a string representation of this check box group.157*/158public String toString() {159return getClass().getName() + "[selectedCheckbox=" + selectedCheckbox + "]";160}161162}163164165