Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/javax/swing/ButtonGroup.java
+++ new/src/share/classes/javax/swing/ButtonGroup.java
1 1 /*
2 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25 package javax.swing;
26 26
27 27 import java.awt.event.*;
28 28 import java.util.Vector;
29 29 import java.util.Enumeration;
30 30 import java.io.Serializable;
31 31
32 32 /**
33 33 * This class is used to create a multiple-exclusion scope for
34 34 * a set of buttons. Creating a set of buttons with the
35 35 * same <code>ButtonGroup</code> object means that
36 36 * turning "on" one of those buttons
37 37 * turns off all other buttons in the group.
38 38 * <p>
39 39 * A <code>ButtonGroup</code> can be used with
40 40 * any set of objects that inherit from <code>AbstractButton</code>.
41 41 * Typically a button group contains instances of
42 42 * <code>JRadioButton</code>,
43 43 * <code>JRadioButtonMenuItem</code>,
↓ open down ↓ |
43 lines elided |
↑ open up ↑ |
44 44 * or <code>JToggleButton</code>.
45 45 * It wouldn't make sense to put an instance of
46 46 * <code>JButton</code> or <code>JMenuItem</code>
47 47 * in a button group
48 48 * because <code>JButton</code> and <code>JMenuItem</code>
49 49 * don't implement the selected state.
50 50 * <p>
51 51 * Initially, all buttons in the group are unselected.
52 52 * <p>
53 53 * For examples and further information on using button groups see
54 - * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>,
54 + * <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>,
55 55 * a section in <em>The Java Tutorial</em>.
56 56 * <p>
57 57 * <strong>Warning:</strong>
58 58 * Serialized objects of this class will not be compatible with
59 59 * future Swing releases. The current serialization support is
60 60 * appropriate for short term storage or RMI between applications running
61 61 * the same version of Swing. As of 1.4, support for long term storage
62 62 * of all JavaBeans™
63 63 * has been added to the <code>java.beans</code> package.
64 64 * Please see {@link java.beans.XMLEncoder}.
65 65 *
66 66 * @author Jeff Dinkins
67 67 */
68 68 @SuppressWarnings("serial")
69 69 public class ButtonGroup implements Serializable {
70 70
71 71 // the list of buttons participating in this group
72 72 protected Vector<AbstractButton> buttons = new Vector<AbstractButton>();
73 73
74 74 /**
75 75 * The current selection.
76 76 */
77 77 ButtonModel selection = null;
78 78
79 79 /**
80 80 * Creates a new <code>ButtonGroup</code>.
81 81 */
82 82 public ButtonGroup() {}
83 83
84 84 /**
85 85 * Adds the button to the group.
86 86 * @param b the button to be added
87 87 */
88 88 public void add(AbstractButton b) {
89 89 if(b == null) {
90 90 return;
91 91 }
92 92 buttons.addElement(b);
93 93
94 94 if (b.isSelected()) {
95 95 if (selection == null) {
96 96 selection = b.getModel();
97 97 } else {
98 98 b.setSelected(false);
99 99 }
100 100 }
101 101
102 102 b.getModel().setGroup(this);
103 103 }
104 104
105 105 /**
106 106 * Removes the button from the group.
107 107 * @param b the button to be removed
108 108 */
109 109 public void remove(AbstractButton b) {
110 110 if(b == null) {
111 111 return;
112 112 }
113 113 buttons.removeElement(b);
114 114 if(b.getModel() == selection) {
115 115 selection = null;
116 116 }
117 117 b.getModel().setGroup(null);
118 118 }
119 119
120 120 /**
121 121 * Clears the selection such that none of the buttons
122 122 * in the <code>ButtonGroup</code> are selected.
123 123 *
124 124 * @since 1.6
125 125 */
126 126 public void clearSelection() {
127 127 if (selection != null) {
128 128 ButtonModel oldSelection = selection;
129 129 selection = null;
130 130 oldSelection.setSelected(false);
131 131 }
132 132 }
133 133
134 134 /**
135 135 * Returns all the buttons that are participating in
136 136 * this group.
137 137 * @return an <code>Enumeration</code> of the buttons in this group
138 138 */
139 139 public Enumeration<AbstractButton> getElements() {
140 140 return buttons.elements();
141 141 }
142 142
143 143 /**
144 144 * Returns the model of the selected button.
145 145 * @return the selected button model
146 146 */
147 147 public ButtonModel getSelection() {
148 148 return selection;
149 149 }
150 150
151 151 /**
152 152 * Sets the selected value for the <code>ButtonModel</code>.
153 153 * Only one button in the group may be selected at a time.
154 154 * @param m the <code>ButtonModel</code>
155 155 * @param b <code>true</code> if this button is to be
156 156 * selected, otherwise <code>false</code>
157 157 */
158 158 public void setSelected(ButtonModel m, boolean b) {
159 159 if (b && m != null && m != selection) {
160 160 ButtonModel oldSelection = selection;
161 161 selection = m;
162 162 if (oldSelection != null) {
163 163 oldSelection.setSelected(false);
164 164 }
165 165 m.setSelected(true);
166 166 }
167 167 }
168 168
169 169 /**
170 170 * Returns whether a <code>ButtonModel</code> is selected.
171 171 * @return <code>true</code> if the button is selected,
172 172 * otherwise returns <code>false</code>
173 173 */
174 174 public boolean isSelected(ButtonModel m) {
175 175 return (m == selection);
176 176 }
177 177
178 178 /**
179 179 * Returns the number of buttons in the group.
180 180 * @return the button count
181 181 * @since 1.3
182 182 */
183 183 public int getButtonCount() {
184 184 if (buttons == null) {
185 185 return 0;
186 186 } else {
187 187 return buttons.size();
188 188 }
189 189 }
190 190
191 191 }
↓ open down ↓ |
127 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX