001 /*
002 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing;
026
027 import java.beans.ConstructorProperties;
028
029 import java.awt.*;
030 import java.awt.event.*;
031 import java.awt.image.*;
032
033 import javax.swing.plaf.*;
034 import javax.swing.event.*;
035 import javax.accessibility.*;
036
037 import java.io.ObjectOutputStream;
038 import java.io.ObjectInputStream;
039 import java.io.IOException;
040
041 /**
042 * An implementation of a "push" button.
043 * <p>
044 * Buttons can be configured, and to some degree controlled, by
045 * <code><a href="Action.html">Action</a></code>s. Using an
046 * <code>Action</code> with a button has many benefits beyond directly
047 * configuring a button. Refer to <a href="Action.html#buttonActions">
048 * Swing Components Supporting <code>Action</code></a> for more
049 * details, and you can find more information in <a
050 * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
051 * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
052 * <p>
053 * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
054 * in <em>The Java Tutorial</em>
055 * for information and examples of using buttons.
056 * <p>
057 * <strong>Warning:</strong> Swing is not thread safe. For more
058 * information see <a
059 * href="package-summary.html#threading">Swing's Threading
060 * Policy</a>.
061 * <p>
062 * <strong>Warning:</strong>
063 * Serialized objects of this class will not be compatible with
064 * future Swing releases. The current serialization support is
065 * appropriate for short term storage or RMI between applications running
066 * the same version of Swing. As of 1.4, support for long term storage
067 * of all JavaBeans<sup><font size="-2">TM</font></sup>
068 * has been added to the <code>java.beans</code> package.
069 * Please see {@link java.beans.XMLEncoder}.
070 *
071 * @beaninfo
072 * attribute: isContainer false
073 * description: An implementation of a \"push\" button.
074 *
075 * @version 1.105 05/05/07
076 * @author Jeff Dinkins
077 */
078 public class JButton extends AbstractButton implements Accessible {
079
080 /**
081 * @see #getUIClassID
082 * @see #readObject
083 */
084 private static final String uiClassID = "ButtonUI";
085
086 /**
087 * Creates a button with no set text or icon.
088 */
089 public JButton() {
090 this (null, null);
091 }
092
093 /**
094 * Creates a button with an icon.
095 *
096 * @param icon the Icon image to display on the button
097 */
098 public JButton(Icon icon) {
099 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 * Returns a string that specifies the name of the L&F class
151 * that renders this component.
152 *
153 * @return the string "ButtonUI"
154 * @see JComponent#getUIClassID
155 * @see UIDefaults#getUI
156 * @beaninfo
157 * expert: true
158 * description: A string that specifies the name of the L&F class.
159 */
160 public String getUIClassID() {
161 return uiClassID;
162 }
163
164 /**
165 * Gets the value of the <code>defaultButton</code> property,
166 * which if <code>true</code> means that this button is the current
167 * default button for its <code>JRootPane</code>.
168 * Most look and feels render the default button
169 * differently, and may potentially provide bindings
170 * to access the default button.
171 *
172 * @return the value of the <code>defaultButton</code> property
173 * @see JRootPane#setDefaultButton
174 * @see #isDefaultCapable
175 * @beaninfo
176 * description: Whether or not this button is the default button
177 */
178 public boolean isDefaultButton() {
179 JRootPane root = SwingUtilities.getRootPane(this );
180 if (root != null) {
181 return root.getDefaultButton() == this ;
182 }
183 return false;
184 }
185
186 /**
187 * Gets the value of the <code>defaultCapable</code> property.
188 *
189 * @return the value of the <code>defaultCapable</code> property
190 * @see #setDefaultCapable
191 * @see #isDefaultButton
192 * @see JRootPane#setDefaultButton
193 */
194 public boolean isDefaultCapable() {
195 return defaultCapable;
196 }
197
198 /**
199 * Sets the <code>defaultCapable</code> property,
200 * which determines whether this button can be
201 * made the default button for its root pane.
202 * The default value of the <code>defaultCapable</code>
203 * property is <code>true</code> unless otherwise
204 * specified by the look and feel.
205 *
206 * @param defaultCapable <code>true</code> if this button will be
207 * capable of being the default button on the
208 * <code>RootPane</code>; otherwise <code>false</code>
209 * @see #isDefaultCapable
210 * @beaninfo
211 * bound: true
212 * attribute: visualUpdate true
213 * description: Whether or not this button can be the default button
214 */
215 public void setDefaultCapable(boolean defaultCapable) {
216 boolean oldDefaultCapable = this .defaultCapable;
217 this .defaultCapable = defaultCapable;
218 firePropertyChange("defaultCapable", oldDefaultCapable,
219 defaultCapable);
220 }
221
222 /**
223 * Overrides <code>JComponent.removeNotify</code> to check if
224 * this button is currently set as the default button on the
225 * <code>RootPane</code>, and if so, sets the <code>RootPane</code>'s
226 * default button to <code>null</code> to ensure the
227 * <code>RootPane</code> doesn't hold onto an invalid button reference.
228 */
229 public void removeNotify() {
230 JRootPane root = SwingUtilities.getRootPane(this );
231 if (root != null && root.getDefaultButton() == this ) {
232 root.setDefaultButton(null);
233 }
234 super .removeNotify();
235 }
236
237 /**
238 * See readObject() and writeObject() in JComponent for more
239 * information about serialization in Swing.
240 */
241 private void writeObject(ObjectOutputStream s) throws IOException {
242 s.defaultWriteObject();
243 if (getUIClassID().equals(uiClassID)) {
244 byte count = JComponent.getWriteObjCounter(this );
245 JComponent.setWriteObjCounter(this , --count);
246 if (count == 0 && ui != null) {
247 ui.installUI(this );
248 }
249 }
250 }
251
252 /**
253 * Returns a string representation of this <code>JButton</code>.
254 * This method is intended to be used only for debugging purposes, and the
255 * content and format of the returned string may vary between
256 * implementations. The returned string may be empty but may not
257 * be <code>null</code>.
258 *
259 * @return a string representation of this <code>JButton</code>
260 */
261 protected String paramString() {
262 String defaultCapableString = (defaultCapable ? "true"
263 : "false");
264
265 return super .paramString() + ",defaultCapable="
266 + defaultCapableString;
267 }
268
269 /////////////////
270 // Accessibility support
271 ////////////////
272
273 /**
274 * Gets the <code>AccessibleContext</code> associated with this
275 * <code>JButton</code>. For <code>JButton</code>s,
276 * the <code>AccessibleContext</code> takes the form of an
277 * <code>AccessibleJButton</code>.
278 * A new <code>AccessibleJButton</code> instance is created if necessary.
279 *
280 * @return an <code>AccessibleJButton</code> that serves as the
281 * <code>AccessibleContext</code> of this <code>JButton</code>
282 * @beaninfo
283 * expert: true
284 * description: The AccessibleContext associated with this Button.
285 */
286 public AccessibleContext getAccessibleContext() {
287 if (accessibleContext == null) {
288 accessibleContext = new AccessibleJButton();
289 }
290 return accessibleContext;
291 }
292
293 /**
294 * This class implements accessibility support for the
295 * <code>JButton</code> class. It provides an implementation of the
296 * Java Accessibility API appropriate to button user-interface
297 * elements.
298 * <p>
299 * <strong>Warning:</strong>
300 * Serialized objects of this class will not be compatible with
301 * future Swing releases. The current serialization support is
302 * appropriate for short term storage or RMI between applications running
303 * the same version of Swing. As of 1.4, support for long term storage
304 * of all JavaBeans<sup><font size="-2">TM</font></sup>
305 * has been added to the <code>java.beans</code> package.
306 * Please see {@link java.beans.XMLEncoder}.
307 */
308 protected class AccessibleJButton extends AccessibleAbstractButton {
309
310 /**
311 * Get the role of this object.
312 *
313 * @return an instance of AccessibleRole describing the role of the
314 * object
315 * @see AccessibleRole
316 */
317 public AccessibleRole getAccessibleRole() {
318 return AccessibleRole.PUSH_BUTTON;
319 }
320 } // inner class AccessibleJButton
321 }
|