KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > ButtonGroup


1 /*
2  * @(#)ButtonGroup.java 1.37 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.event.*;
10 import java.util.Vector JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.io.Serializable JavaDoc;
13  
14 /**
15  * This class is used to create a multiple-exclusion scope for
16  * a set of buttons. Creating a set of buttons with the
17  * same <code>ButtonGroup</code> object means that
18  * turning "on" one of those buttons
19  * turns off all other buttons in the group.
20  * <p>
21  * A <code>ButtonGroup</code> can be used with
22  * any set of objects that inherit from <code>AbstractButton</code>.
23  * Typically a button group contains instances of
24  * <code>JRadioButton</code>,
25  * <code>JRadioButtonMenuItem</code>,
26  * or <code>JToggleButton</code>.
27  * It wouldn't make sense to put an instance of
28  * <code>JButton</code> or <code>JMenuItem</code>
29  * in a button group
30  * because <code>JButton</code> and <code>JMenuItem</code>
31  * don't implement the selected state.
32  * <p>
33  * Initially, all buttons in the group are unselected. Once any button is
34  * selected, one button is always selected in the group. There is no way
35  * to turn a button programmatically to "off", in order to clear the button
36  * group. To give the appearance of "none selected", add an invisible radio
37  * button to the group and then programmatically select that button to
38  * turn off all the displayed radio buttons. For example, a normal button
39  * with the label "none" could be wired to select the invisible radio button.
40  * <p>
41  * For examples and further information on using button groups see
42  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>,
43  * a section in <em>The Java Tutorial</em>.
44  * <p>
45  * <strong>Warning:</strong>
46  * Serialized objects of this class will not be compatible with
47  * future Swing releases. The current serialization support is
48  * appropriate for short term storage or RMI between applications running
49  * the same version of Swing. As of 1.4, support for long term storage
50  * of all JavaBeans<sup><font size="-2">TM</font></sup>
51  * has been added to the <code>java.beans</code> package.
52  * Please see {@link java.beans.XMLEncoder}.
53  *
54  * @version 1.37 05/05/04
55  * @author Jeff Dinkins
56  */

57 public class ButtonGroup implements Serializable JavaDoc {
58
59     // the list of buttons participating in this group
60
protected Vector JavaDoc<AbstractButton JavaDoc> buttons = new Vector JavaDoc();
61
62     /**
63      * The current selection.
64      */

65     ButtonModel JavaDoc selection = null;
66
67     /**
68      * Creates a new <code>ButtonGroup</code>.
69      */

70     public ButtonGroup() {}
71
72     /**
73      * Adds the button to the group.
74      * @param b the button to be added
75      */

76     public void add(AbstractButton JavaDoc b) {
77         if(b == null) {
78             return;
79         }
80         buttons.addElement(b);
81
82         if (b.isSelected()) {
83             if (selection == null) {
84                 selection = b.getModel();
85             } else {
86                 b.setSelected(false);
87             }
88         }
89
90         b.getModel().setGroup(this);
91     }
92  
93     /**
94      * Removes the button from the group.
95      * @param b the button to be removed
96      */

97     public void remove(AbstractButton JavaDoc b) {
98         if(b == null) {
99             return;
100         }
101         buttons.removeElement(b);
102         if(b.getModel() == selection) {
103             selection = null;
104         }
105         b.getModel().setGroup(null);
106     }
107
108     /**
109      * Returns all the buttons that are participating in
110      * this group.
111      * @return an <code>Enumeration</code> of the buttons in this group
112      */

113     public Enumeration JavaDoc<AbstractButton JavaDoc> getElements() {
114         return buttons.elements();
115     }
116
117     /**
118      * Returns the model of the selected button.
119      * @return the selected button model
120      */

121     public ButtonModel JavaDoc getSelection() {
122         return selection;
123     }
124
125     /**
126      * Sets the selected value for the <code>ButtonModel</code>.
127      * Only one button in the group may be selected at a time.
128      * @param m the <code>ButtonModel</code>
129      * @param b <code>true</code> if this button is to be
130      * selected, otherwise <code>false</code>
131      */

132     public void setSelected(ButtonModel JavaDoc m, boolean b) {
133         if (b && m != null && m != selection) {
134             ButtonModel JavaDoc oldSelection = selection;
135             selection = m;
136             if (oldSelection != null) {
137                 oldSelection.setSelected(false);
138             }
139             m.setSelected(true);
140         }
141     }
142
143     /**
144      * Returns whether a <code>ButtonModel</code> is selected.
145      * @return <code>true</code> if the button is selected,
146      * otherwise returns <code>false</code>
147      */

148     public boolean isSelected(ButtonModel JavaDoc m) {
149         return (m == selection);
150     }
151
152     /**
153      * Returns the number of buttons in the group.
154      * @return the button count
155      * @since 1.3
156      */

157     public int getButtonCount() {
158     if (buttons == null) {
159         return 0;
160     } else {
161         return buttons.size();
162     }
163     }
164
165 }
166
Popular Tags