author | cl |
Mon, 07 Oct 2013 11:34:44 -0700 | |
changeset 20458 | f2423fb3fd19 |
parent 20455 | f6f9a0c2796b |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
package javax.swing; |
|
26 |
||
27 |
import java.awt.event.*; |
|
28 |
import java.util.Vector; |
|
29 |
import java.util.Enumeration; |
|
30 |
import java.io.Serializable; |
|
31 |
||
32 |
/** |
|
33 |
* This class is used to create a multiple-exclusion scope for |
|
34 |
* a set of buttons. Creating a set of buttons with the |
|
35 |
* same <code>ButtonGroup</code> object means that |
|
36 |
* turning "on" one of those buttons |
|
37 |
* turns off all other buttons in the group. |
|
38 |
* <p> |
|
39 |
* A <code>ButtonGroup</code> can be used with |
|
40 |
* any set of objects that inherit from <code>AbstractButton</code>. |
|
41 |
* Typically a button group contains instances of |
|
42 |
* <code>JRadioButton</code>, |
|
43 |
* <code>JRadioButtonMenuItem</code>, |
|
44 |
* or <code>JToggleButton</code>. |
|
45 |
* It wouldn't make sense to put an instance of |
|
46 |
* <code>JButton</code> or <code>JMenuItem</code> |
|
47 |
* in a button group |
|
48 |
* because <code>JButton</code> and <code>JMenuItem</code> |
|
49 |
* don't implement the selected state. |
|
50 |
* <p> |
|
51 |
* Initially, all buttons in the group are unselected. |
|
52 |
* <p> |
|
53 |
* For examples and further information on using button groups see |
|
20455
f6f9a0c2796b
8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents:
11268
diff
changeset
|
54 |
* <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>, |
2 | 55 |
* a section in <em>The Java Tutorial</em>. |
56 |
* <p> |
|
57 |
* <strong>Warning:</strong> |
|
58 |
* Serialized objects of this class will not be compatible with |
|
59 |
* future Swing releases. The current serialization support is |
|
60 |
* appropriate for short term storage or RMI between applications running |
|
61 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 62 |
* of all JavaBeans™ |
2 | 63 |
* has been added to the <code>java.beans</code> package. |
64 |
* Please see {@link java.beans.XMLEncoder}. |
|
65 |
* |
|
66 |
* @author Jeff Dinkins |
|
67 |
*/ |
|
11268 | 68 |
@SuppressWarnings("serial") |
2 | 69 |
public class ButtonGroup implements Serializable { |
70 |
||
71 |
// the list of buttons participating in this group |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
72 |
protected Vector<AbstractButton> buttons = new Vector<AbstractButton>(); |
2 | 73 |
|
74 |
/** |
|
75 |
* The current selection. |
|
76 |
*/ |
|
77 |
ButtonModel selection = null; |
|
78 |
||
79 |
/** |
|
80 |
* Creates a new <code>ButtonGroup</code>. |
|
81 |
*/ |
|
82 |
public ButtonGroup() {} |
|
83 |
||
84 |
/** |
|
85 |
* Adds the button to the group. |
|
86 |
* @param b the button to be added |
|
87 |
*/ |
|
88 |
public void add(AbstractButton b) { |
|
89 |
if(b == null) { |
|
90 |
return; |
|
91 |
} |
|
92 |
buttons.addElement(b); |
|
93 |
||
94 |
if (b.isSelected()) { |
|
95 |
if (selection == null) { |
|
96 |
selection = b.getModel(); |
|
97 |
} else { |
|
98 |
b.setSelected(false); |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
b.getModel().setGroup(this); |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Removes the button from the group. |
|
107 |
* @param b the button to be removed |
|
108 |
*/ |
|
109 |
public void remove(AbstractButton b) { |
|
110 |
if(b == null) { |
|
111 |
return; |
|
112 |
} |
|
113 |
buttons.removeElement(b); |
|
114 |
if(b.getModel() == selection) { |
|
115 |
selection = null; |
|
116 |
} |
|
117 |
b.getModel().setGroup(null); |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Clears the selection such that none of the buttons |
|
122 |
* in the <code>ButtonGroup</code> are selected. |
|
123 |
* |
|
124 |
* @since 1.6 |
|
125 |
*/ |
|
126 |
public void clearSelection() { |
|
127 |
if (selection != null) { |
|
128 |
ButtonModel oldSelection = selection; |
|
129 |
selection = null; |
|
130 |
oldSelection.setSelected(false); |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* Returns all the buttons that are participating in |
|
136 |
* this group. |
|
137 |
* @return an <code>Enumeration</code> of the buttons in this group |
|
138 |
*/ |
|
139 |
public Enumeration<AbstractButton> getElements() { |
|
140 |
return buttons.elements(); |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Returns the model of the selected button. |
|
145 |
* @return the selected button model |
|
146 |
*/ |
|
147 |
public ButtonModel getSelection() { |
|
148 |
return selection; |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* Sets the selected value for the <code>ButtonModel</code>. |
|
153 |
* Only one button in the group may be selected at a time. |
|
154 |
* @param m the <code>ButtonModel</code> |
|
155 |
* @param b <code>true</code> if this button is to be |
|
156 |
* selected, otherwise <code>false</code> |
|
157 |
*/ |
|
158 |
public void setSelected(ButtonModel m, boolean b) { |
|
159 |
if (b && m != null && m != selection) { |
|
160 |
ButtonModel oldSelection = selection; |
|
161 |
selection = m; |
|
162 |
if (oldSelection != null) { |
|
163 |
oldSelection.setSelected(false); |
|
164 |
} |
|
165 |
m.setSelected(true); |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* Returns whether a <code>ButtonModel</code> is selected. |
|
171 |
* @return <code>true</code> if the button is selected, |
|
172 |
* otherwise returns <code>false</code> |
|
173 |
*/ |
|
174 |
public boolean isSelected(ButtonModel m) { |
|
175 |
return (m == selection); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Returns the number of buttons in the group. |
|
180 |
* @return the button count |
|
181 |
* @since 1.3 |
|
182 |
*/ |
|
183 |
public int getButtonCount() { |
|
184 |
if (buttons == null) { |
|
185 |
return 0; |
|
186 |
} else { |
|
187 |
return buttons.size(); |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
} |