Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/ButtonGroup.java
38829 views
/*1* Copyright (c) 1997, 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 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 JavaBeans™62* has been added to the <code>java.beans</code> package.63* Please see {@link java.beans.XMLEncoder}.64*65* @author Jeff Dinkins66*/67@SuppressWarnings("serial")68public class ButtonGroup implements Serializable {6970// the list of buttons participating in this group71protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();7273/**74* The current selection.75*/76ButtonModel selection = null;7778/**79* Creates a new <code>ButtonGroup</code>.80*/81public ButtonGroup() {}8283/**84* Adds the button to the group.85* @param b the button to be added86*/87public void add(AbstractButton b) {88if(b == null) {89return;90}91buttons.addElement(b);9293if (b.isSelected()) {94if (selection == null) {95selection = b.getModel();96} else {97b.setSelected(false);98}99}100101b.getModel().setGroup(this);102}103104/**105* Removes the button from the group.106* @param b the button to be removed107*/108public void remove(AbstractButton b) {109if(b == null) {110return;111}112buttons.removeElement(b);113if(b.getModel() == selection) {114selection = null;115}116b.getModel().setGroup(null);117}118119/**120* Clears the selection such that none of the buttons121* in the <code>ButtonGroup</code> are selected.122*123* @since 1.6124*/125public void clearSelection() {126if (selection != null) {127ButtonModel oldSelection = selection;128selection = null;129oldSelection.setSelected(false);130}131}132133/**134* Returns all the buttons that are participating in135* this group.136* @return an <code>Enumeration</code> of the buttons in this group137*/138public Enumeration<AbstractButton> getElements() {139return buttons.elements();140}141142/**143* Returns the model of the selected button.144* @return the selected button model145*/146public ButtonModel getSelection() {147return selection;148}149150/**151* Sets the selected value for the <code>ButtonModel</code>.152* Only one button in the group may be selected at a time.153* @param m the <code>ButtonModel</code>154* @param b <code>true</code> if this button is to be155* selected, otherwise <code>false</code>156*/157public void setSelected(ButtonModel m, boolean b) {158if (b && m != null && m != selection) {159ButtonModel oldSelection = selection;160selection = m;161if (oldSelection != null) {162oldSelection.setSelected(false);163}164m.setSelected(true);165}166}167168/**169* Returns whether a <code>ButtonModel</code> is selected.170* @return <code>true</code> if the button is selected,171* otherwise returns <code>false</code>172*/173public boolean isSelected(ButtonModel m) {174return (m == selection);175}176177/**178* Returns the number of buttons in the group.179* @return the button count180* @since 1.3181*/182public int getButtonCount() {183if (buttons == null) {184return 0;185} else {186return buttons.size();187}188}189190}191192193