Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/CheckboxGroup.java
38829 views
/*1* Copyright (c) 1995, 2013, 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*/24package java.awt;2526/**27* The <code>CheckboxGroup</code> class is used to group together28* a set of <code>Checkbox</code> buttons.29* <p>30* Exactly one check box button in a <code>CheckboxGroup</code> can31* be in the "on" state at any given time. Pushing any32* button sets its state to "on" and forces any other button that33* is in the "on" state into the "off" state.34* <p>35* The following code example produces a new check box group,36* with three check boxes:37*38* <hr><blockquote><pre>39* setLayout(new GridLayout(3, 1));40* CheckboxGroup cbg = new CheckboxGroup();41* add(new Checkbox("one", cbg, true));42* add(new Checkbox("two", cbg, false));43* add(new Checkbox("three", cbg, false));44* </pre></blockquote><hr>45* <p>46* This image depicts the check box group created by this example:47* <p>48* <img src="doc-files/CheckboxGroup-1.gif"49* alt="Shows three checkboxes, arranged vertically, labeled one, two, and three. Checkbox one is in the on state."50* style="float:center; margin: 7px 10px;">51* <p>52* @author Sami Shaio53* @see java.awt.Checkbox54* @since JDK1.055*/56public class CheckboxGroup implements java.io.Serializable {57/**58* The current choice.59* @serial60* @see #getCurrent()61* @see #setCurrent(Checkbox)62*/63Checkbox selectedCheckbox = null;6465/*66* JDK 1.1 serialVersionUID67*/68private static final long serialVersionUID = 3729780091441768983L;6970/**71* Creates a new instance of <code>CheckboxGroup</code>.72*/73public CheckboxGroup() {74}7576/**77* Gets the current choice from this check box group.78* The current choice is the check box in this79* group that is currently in the "on" state,80* or <code>null</code> if all check boxes in the81* group are off.82* @return the check box that is currently in the83* "on" state, or <code>null</code>.84* @see java.awt.Checkbox85* @see java.awt.CheckboxGroup#setSelectedCheckbox86* @since JDK1.187*/88public Checkbox getSelectedCheckbox() {89return getCurrent();90}9192/**93* @deprecated As of JDK version 1.1,94* replaced by <code>getSelectedCheckbox()</code>.95*/96@Deprecated97public Checkbox getCurrent() {98return selectedCheckbox;99}100101/**102* Sets the currently selected check box in this group103* to be the specified check box.104* This method sets the state of that check box to "on" and105* sets all other check boxes in the group to be off.106* <p>107* If the check box argument is <tt>null</tt>, all check boxes108* in this check box group are deselected. If the check box argument109* belongs to a different check box group, this method does110* nothing.111* @param box the <code>Checkbox</code> to set as the112* current selection.113* @see java.awt.Checkbox114* @see java.awt.CheckboxGroup#getSelectedCheckbox115* @since JDK1.1116*/117public void setSelectedCheckbox(Checkbox box) {118setCurrent(box);119}120121/**122* @deprecated As of JDK version 1.1,123* replaced by <code>setSelectedCheckbox(Checkbox)</code>.124*/125@Deprecated126public synchronized void setCurrent(Checkbox box) {127if (box != null && box.group != this) {128return;129}130Checkbox oldChoice = this.selectedCheckbox;131this.selectedCheckbox = box;132if (oldChoice != null && oldChoice != box && oldChoice.group == this) {133oldChoice.setState(false);134}135if (box != null && oldChoice != box && !box.getState()) {136box.setStateInternal(true);137}138}139140/**141* Returns a string representation of this check box group,142* including the value of its current selection.143* @return a string representation of this check box group.144*/145public String toString() {146return getClass().getName() + "[selectedCheckbox=" + selectedCheckbox + "]";147}148149}150151152