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