Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/javax/swing/JButton.java
+++ new/src/share/classes/javax/swing/JButton.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.beans.ConstructorProperties;
28 28
29 29 import java.awt.*;
30 30 import java.awt.event.*;
31 31 import java.awt.image.*;
32 32
33 33 import javax.swing.plaf.*;
34 34 import javax.swing.event.*;
35 35 import javax.accessibility.*;
36 36
37 37 import java.io.ObjectOutputStream;
38 38 import java.io.ObjectInputStream;
39 39 import java.io.IOException;
40 40
↓ open down ↓ |
40 lines elided |
↑ open up ↑ |
41 41
42 42 /**
43 43 * An implementation of a "push" button.
44 44 * <p>
45 45 * Buttons can be configured, and to some degree controlled, by
46 46 * <code><a href="Action.html">Action</a></code>s. Using an
47 47 * <code>Action</code> with a button has many benefits beyond directly
48 48 * configuring a button. Refer to <a href="Action.html#buttonActions">
49 49 * Swing Components Supporting <code>Action</code></a> for more
50 50 * details, and you can find more information in <a
51 - * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
51 + * href="https://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How
52 52 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
53 53 * <p>
54 - * See <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
54 + * See <a href="https://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
55 55 * in <em>The Java Tutorial</em>
56 56 * for information and examples of using buttons.
57 57 * <p>
58 58 * <strong>Warning:</strong> Swing is not thread safe. For more
59 59 * information see <a
60 60 * href="package-summary.html#threading">Swing's Threading
61 61 * Policy</a>.
62 62 * <p>
63 63 * <strong>Warning:</strong>
64 64 * Serialized objects of this class will not be compatible with
65 65 * future Swing releases. The current serialization support is
66 66 * appropriate for short term storage or RMI between applications running
67 67 * the same version of Swing. As of 1.4, support for long term storage
68 68 * of all JavaBeans™
69 69 * has been added to the <code>java.beans</code> package.
70 70 * Please see {@link java.beans.XMLEncoder}.
71 71 *
72 72 * @beaninfo
73 73 * attribute: isContainer false
74 74 * description: An implementation of a \"push\" button.
75 75 *
76 76 * @author Jeff Dinkins
77 77 */
78 78 @SuppressWarnings("serial")
79 79 public class JButton extends AbstractButton implements Accessible {
80 80
81 81 /**
82 82 * @see #getUIClassID
83 83 * @see #readObject
84 84 */
85 85 private static final String uiClassID = "ButtonUI";
86 86
87 87 /**
88 88 * Creates a button with no set text or icon.
89 89 */
90 90 public JButton() {
91 91 this(null, null);
92 92 }
93 93
94 94 /**
95 95 * Creates a button with an icon.
96 96 *
97 97 * @param icon the Icon image to display on the button
98 98 */
99 99 public JButton(Icon icon) {
100 100 this(null, icon);
101 101 }
102 102
103 103 /**
104 104 * Creates a button with text.
105 105 *
106 106 * @param text the text of the button
107 107 */
108 108 @ConstructorProperties({"text"})
109 109 public JButton(String text) {
110 110 this(text, null);
111 111 }
112 112
113 113 /**
114 114 * Creates a button where properties are taken from the
115 115 * <code>Action</code> supplied.
116 116 *
117 117 * @param a the <code>Action</code> used to specify the new button
118 118 *
119 119 * @since 1.3
120 120 */
121 121 public JButton(Action a) {
122 122 this();
123 123 setAction(a);
124 124 }
125 125
126 126 /**
127 127 * Creates a button with initial text and an icon.
128 128 *
129 129 * @param text the text of the button
130 130 * @param icon the Icon image to display on the button
131 131 */
132 132 public JButton(String text, Icon icon) {
133 133 // Create the model
134 134 setModel(new DefaultButtonModel());
135 135
136 136 // initialize
137 137 init(text, icon);
138 138 }
139 139
140 140 /**
141 141 * Resets the UI property to a value from the current look and
142 142 * feel.
143 143 *
144 144 * @see JComponent#updateUI
145 145 */
146 146 public void updateUI() {
147 147 setUI((ButtonUI)UIManager.getUI(this));
148 148 }
149 149
150 150
151 151 /**
152 152 * Returns a string that specifies the name of the L&F class
153 153 * that renders this component.
154 154 *
155 155 * @return the string "ButtonUI"
156 156 * @see JComponent#getUIClassID
157 157 * @see UIDefaults#getUI
158 158 * @beaninfo
159 159 * expert: true
160 160 * description: A string that specifies the name of the L&F class.
161 161 */
162 162 public String getUIClassID() {
163 163 return uiClassID;
164 164 }
165 165
166 166
167 167 /**
168 168 * Gets the value of the <code>defaultButton</code> property,
169 169 * which if <code>true</code> means that this button is the current
170 170 * default button for its <code>JRootPane</code>.
171 171 * Most look and feels render the default button
172 172 * differently, and may potentially provide bindings
173 173 * to access the default button.
174 174 *
175 175 * @return the value of the <code>defaultButton</code> property
176 176 * @see JRootPane#setDefaultButton
177 177 * @see #isDefaultCapable
178 178 * @beaninfo
179 179 * description: Whether or not this button is the default button
180 180 */
181 181 public boolean isDefaultButton() {
182 182 JRootPane root = SwingUtilities.getRootPane(this);
183 183 if (root != null) {
184 184 return root.getDefaultButton() == this;
185 185 }
186 186 return false;
187 187 }
188 188
189 189 /**
190 190 * Gets the value of the <code>defaultCapable</code> property.
191 191 *
192 192 * @return the value of the <code>defaultCapable</code> property
193 193 * @see #setDefaultCapable
194 194 * @see #isDefaultButton
195 195 * @see JRootPane#setDefaultButton
196 196 */
197 197 public boolean isDefaultCapable() {
198 198 return defaultCapable;
199 199 }
200 200
201 201 /**
202 202 * Sets the <code>defaultCapable</code> property,
203 203 * which determines whether this button can be
204 204 * made the default button for its root pane.
205 205 * The default value of the <code>defaultCapable</code>
206 206 * property is <code>true</code> unless otherwise
207 207 * specified by the look and feel.
208 208 *
209 209 * @param defaultCapable <code>true</code> if this button will be
210 210 * capable of being the default button on the
211 211 * <code>RootPane</code>; otherwise <code>false</code>
212 212 * @see #isDefaultCapable
213 213 * @beaninfo
214 214 * bound: true
215 215 * attribute: visualUpdate true
216 216 * description: Whether or not this button can be the default button
217 217 */
218 218 public void setDefaultCapable(boolean defaultCapable) {
219 219 boolean oldDefaultCapable = this.defaultCapable;
220 220 this.defaultCapable = defaultCapable;
221 221 firePropertyChange("defaultCapable", oldDefaultCapable, defaultCapable);
222 222 }
223 223
224 224 /**
225 225 * Overrides <code>JComponent.removeNotify</code> to check if
226 226 * this button is currently set as the default button on the
227 227 * <code>RootPane</code>, and if so, sets the <code>RootPane</code>'s
228 228 * default button to <code>null</code> to ensure the
229 229 * <code>RootPane</code> doesn't hold onto an invalid button reference.
230 230 */
231 231 public void removeNotify() {
232 232 JRootPane root = SwingUtilities.getRootPane(this);
233 233 if (root != null && root.getDefaultButton() == this) {
234 234 root.setDefaultButton(null);
235 235 }
236 236 super.removeNotify();
237 237 }
238 238
239 239 /**
240 240 * See readObject() and writeObject() in JComponent for more
241 241 * information about serialization in Swing.
242 242 */
243 243 private void writeObject(ObjectOutputStream s) throws IOException {
244 244 s.defaultWriteObject();
245 245 if (getUIClassID().equals(uiClassID)) {
246 246 byte count = JComponent.getWriteObjCounter(this);
247 247 JComponent.setWriteObjCounter(this, --count);
248 248 if (count == 0 && ui != null) {
249 249 ui.installUI(this);
250 250 }
251 251 }
252 252 }
253 253
254 254
255 255 /**
256 256 * Returns a string representation of this <code>JButton</code>.
257 257 * This method is intended to be used only for debugging purposes, and the
258 258 * content and format of the returned string may vary between
259 259 * implementations. The returned string may be empty but may not
260 260 * be <code>null</code>.
261 261 *
262 262 * @return a string representation of this <code>JButton</code>
263 263 */
264 264 protected String paramString() {
265 265 String defaultCapableString = (defaultCapable ? "true" : "false");
266 266
267 267 return super.paramString() +
268 268 ",defaultCapable=" + defaultCapableString;
269 269 }
270 270
271 271
272 272 /////////////////
273 273 // Accessibility support
274 274 ////////////////
275 275
276 276 /**
277 277 * Gets the <code>AccessibleContext</code> associated with this
278 278 * <code>JButton</code>. For <code>JButton</code>s,
279 279 * the <code>AccessibleContext</code> takes the form of an
280 280 * <code>AccessibleJButton</code>.
281 281 * A new <code>AccessibleJButton</code> instance is created if necessary.
282 282 *
283 283 * @return an <code>AccessibleJButton</code> that serves as the
284 284 * <code>AccessibleContext</code> of this <code>JButton</code>
285 285 * @beaninfo
286 286 * expert: true
287 287 * description: The AccessibleContext associated with this Button.
288 288 */
289 289 public AccessibleContext getAccessibleContext() {
290 290 if (accessibleContext == null) {
291 291 accessibleContext = new AccessibleJButton();
292 292 }
293 293 return accessibleContext;
294 294 }
295 295
296 296 /**
297 297 * This class implements accessibility support for the
298 298 * <code>JButton</code> class. It provides an implementation of the
299 299 * Java Accessibility API appropriate to button user-interface
300 300 * elements.
301 301 * <p>
302 302 * <strong>Warning:</strong>
303 303 * Serialized objects of this class will not be compatible with
304 304 * future Swing releases. The current serialization support is
305 305 * appropriate for short term storage or RMI between applications running
306 306 * the same version of Swing. As of 1.4, support for long term storage
307 307 * of all JavaBeans™
308 308 * has been added to the <code>java.beans</code> package.
309 309 * Please see {@link java.beans.XMLEncoder}.
310 310 */
311 311 @SuppressWarnings("serial")
312 312 protected class AccessibleJButton extends AccessibleAbstractButton {
313 313
314 314 /**
315 315 * Get the role of this object.
316 316 *
317 317 * @return an instance of AccessibleRole describing the role of the
318 318 * object
319 319 * @see AccessibleRole
320 320 */
321 321 public AccessibleRole getAccessibleRole() {
322 322 return AccessibleRole.PUSH_BUTTON;
323 323 }
324 324 } // inner class AccessibleJButton
325 325 }
↓ open down ↓ |
261 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX