1 /* 2 * Copyright (c) 1995, 2019, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.awt; 26 27 /** 28 * The {@code CheckboxGroup} class is used to group together 29 * a set of {@code Checkbox} buttons. 30 * <p> 31 * Exactly one check box button in a {@code CheckboxGroup} 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 * 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" alt="Shows three checkboxes, 50 * arranged vertically, labeled one, two, and three. Checkbox one is in the on 51 * state." style="margin: 7px 10px;"> 52 * 53 * @author Sami Shaio 54 * @see java.awt.Checkbox 55 * @since 1.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}. 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} 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}. 85 * @see java.awt.Checkbox 86 * @see java.awt.CheckboxGroup#setSelectedCheckbox 87 * @since 1.1 88 */ 89 public Checkbox getSelectedCheckbox() { 90 return getCurrent(); 91 } 92 93 /** 94 * Returns the current choice from this check box group 95 * or {@code null} if none of checkboxes are selected. 96 * 97 * @return the selected checkbox 98 * @deprecated As of JDK version 1.1, 99 * replaced by {@code getSelectedCheckbox()}. 100 */ 101 @Deprecated 102 public Checkbox getCurrent() { 103 return selectedCheckbox; 104 } 105 106 /** 107 * Sets the currently selected check box in this group 108 * to be the specified check box. 109 * This method sets the state of that check box to "on" and 110 * sets all other check boxes in the group to be off. 111 * <p> 112 * If the check box argument is {@code null}, all check boxes 113 * in this check box group are deselected. If the check box argument 114 * belongs to a different check box group, this method does 115 * nothing. 116 * @param box the {@code Checkbox} to set as the 117 * current selection. 118 * @see java.awt.Checkbox 119 * @see java.awt.CheckboxGroup#getSelectedCheckbox 120 * @since 1.1 121 */ 122 public void setSelectedCheckbox(Checkbox box) { 123 setCurrent(box); 124 } 125 126 /** 127 * Sets the currently selected check box in this group 128 * to be the specified check box and unsets all others. 129 * 130 * @param box the {@code Checkbox} to set as the 131 * current selection. 132 * @deprecated As of JDK version 1.1, 133 * replaced by {@code setSelectedCheckbox(Checkbox)}. 134 */ 135 @Deprecated 136 public synchronized void setCurrent(Checkbox box) { 137 if (box != null && box.group != this) { 138 return; 139 } 140 Checkbox oldChoice = this.selectedCheckbox; 141 this.selectedCheckbox = box; 142 if (oldChoice != null && oldChoice != box && oldChoice.group == this) { 143 oldChoice.setState(false); 144 } 145 if (box != null && oldChoice != box && !box.getState()) { 146 box.setStateInternal(true); 147 } 148 } 149 150 /** 151 * Returns a string representation of this check box group, 152 * including the value of its current selection. 153 * @return a string representation of this check box group. 154 */ 155 public String toString() { 156 return getClass().getName() + "[selectedCheckbox=" + selectedCheckbox + "]"; 157 } 158 159 }