Path: blob/master/src/java.desktop/share/classes/javax/swing/ButtonGroup.java
41153 views
/*1* Copyright (c) 1997, 2015, 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 javax.swing;2526import java.awt.event.*;27import java.util.Vector;28import java.util.Enumeration;29import java.io.Serializable;3031/**32* This class is used to create a multiple-exclusion scope for33* a set of buttons. Creating a set of buttons with the34* same <code>ButtonGroup</code> object means that35* turning "on" one of those buttons36* turns off all other buttons in the group.37* <p>38* A <code>ButtonGroup</code> can be used with39* any set of objects that inherit from <code>AbstractButton</code>.40* Typically a button group contains instances of41* <code>JRadioButton</code>,42* <code>JRadioButtonMenuItem</code>,43* or <code>JToggleButton</code>.44* It wouldn't make sense to put an instance of45* <code>JButton</code> or <code>JMenuItem</code>46* in a button group47* because <code>JButton</code> and <code>JMenuItem</code>48* don't implement the selected state.49* <p>50* Initially, all buttons in the group are unselected.51* <p>52* For examples and further information on using button groups see53* <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>,54* a section in <em>The Java Tutorial</em>.55* <p>56* <strong>Warning:</strong>57* Serialized objects of this class will not be compatible with58* future Swing releases. The current serialization support is59* appropriate for short term storage or RMI between applications running60* the same version of Swing. As of 1.4, support for long term storage61* of all JavaBeans62* has been added to the <code>java.beans</code> package.63* Please see {@link java.beans.XMLEncoder}.64*65* @author Jeff Dinkins66* @since 1.267*/68@SuppressWarnings("serial")69public class ButtonGroup implements Serializable {7071/**72* The list of buttons participating in this group.73*/74protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();7576/**77* The current selection.78*/79ButtonModel selection = null;8081/**82* Creates a new <code>ButtonGroup</code>.83*/84public ButtonGroup() {}8586/**87* Adds the button to the group.88* @param b the button to be added89*/90public void add(AbstractButton b) {91if(b == null) {92return;93}94buttons.addElement(b);9596if (b.isSelected()) {97if (selection == null) {98selection = b.getModel();99} else {100b.setSelected(false);101}102}103104b.getModel().setGroup(this);105}106107/**108* Removes the button from the group.109* @param b the button to be removed110*/111public void remove(AbstractButton b) {112if(b == null) {113return;114}115buttons.removeElement(b);116if(b.getModel() == selection) {117selection = null;118}119b.getModel().setGroup(null);120}121122/**123* Clears the selection such that none of the buttons124* in the <code>ButtonGroup</code> are selected.125*126* @since 1.6127*/128public void clearSelection() {129if (selection != null) {130ButtonModel oldSelection = selection;131selection = null;132oldSelection.setSelected(false);133}134}135136/**137* Returns all the buttons that are participating in138* this group.139* @return an <code>Enumeration</code> of the buttons in this group140*/141public Enumeration<AbstractButton> getElements() {142return buttons.elements();143}144145/**146* Returns the model of the selected button.147* @return the selected button model148*/149public ButtonModel getSelection() {150return selection;151}152153/**154* Sets the selected value for the <code>ButtonModel</code>.155* Only one button in the group may be selected at a time.156* @param m the <code>ButtonModel</code>157* @param b <code>true</code> if this button is to be158* selected, otherwise <code>false</code>159*/160public void setSelected(ButtonModel m, boolean b) {161if (b && m != null && m != selection) {162ButtonModel oldSelection = selection;163selection = m;164if (oldSelection != null) {165oldSelection.setSelected(false);166}167m.setSelected(true);168}169}170171/**172* Returns whether a {@code ButtonModel} is selected.173*174* @param m an isntance of {@code ButtonModel}175* @return {@code true} if the button is selected,176* otherwise returns {@code false}177*/178public boolean isSelected(ButtonModel m) {179return (m == selection);180}181182/**183* Returns the number of buttons in the group.184* @return the button count185* @since 1.3186*/187public int getButtonCount() {188if (buttons == null) {189return 0;190} else {191return buttons.size();192}193}194195}196197198