KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JButton


1 /*
2  * @(#)JButton.java 1.97 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.util.EventListener JavaDoc;
10
11 import java.awt.*;
12 import java.awt.event.*;
13 import java.awt.image.*;
14
15 import javax.swing.plaf.*;
16 import javax.swing.event.*;
17 import javax.accessibility.*;
18
19 import java.io.ObjectOutputStream JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23
24 /**
25  * An implementation of a "push" button.
26  * See <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
27  * in <em>The Java Tutorial</em>
28  * for information and examples of using buttons.
29  * <p>
30  * <strong>Warning:</strong>
31  * Serialized objects of this class will not be compatible with
32  * future Swing releases. The current serialization support is
33  * appropriate for short term storage or RMI between applications running
34  * the same version of Swing. As of 1.4, support for long term storage
35  * of all JavaBeans<sup><font size="-2">TM</font></sup>
36  * has been added to the <code>java.beans</code> package.
37  * Please see {@link java.beans.XMLEncoder}.
38  *
39  * @beaninfo
40  * attribute: isContainer false
41  * description: An implementation of a \"push\" button.
42  *
43  * @version 1.97 12/19/03
44  * @author Jeff Dinkins
45  */

46 public class JButton extends AbstractButton JavaDoc implements Accessible {
47
48     /**
49      * @see #getUIClassID
50      * @see #readObject
51      */

52     private static final String JavaDoc uiClassID = "ButtonUI";
53
54     /**
55      * Creates a button with no set text or icon.
56      */

57     public JButton() {
58         this(null, null);
59     }
60     
61     /**
62      * Creates a button with an icon.
63      *
64      * @param icon the Icon image to display on the button
65      */

66     public JButton(Icon JavaDoc icon) {
67         this(null, icon);
68     }
69     
70     /**
71      * Creates a button with text.
72      *
73      * @param text the text of the button
74      */

75     public JButton(String JavaDoc text) {
76         this(text, null);
77     }
78     
79     /**
80      * Creates a button where properties are taken from the
81      * <code>Action</code> supplied.
82      *
83      * @param a the <code>Action</code> used to specify the new button
84      *
85      * @since 1.3
86      */

87     public JButton(Action JavaDoc a) {
88         this();
89     setAction(a);
90     }
91
92     /**
93      * Creates a button with initial text and an icon.
94      *
95      * @param text the text of the button
96      * @param icon the Icon image to display on the button
97      */

98     public JButton(String JavaDoc text, Icon JavaDoc icon) {
99         // Create the model
100
setModel(new DefaultButtonModel JavaDoc());
101
102         // initialize
103
init(text, icon);
104     }
105
106     /**
107      * Resets the UI property to a value from the current look and
108      * feel.
109      *
110      * @see JComponent#updateUI
111      */

112     public void updateUI() {
113         setUI((ButtonUI)UIManager.getUI(this));
114     }
115     
116
117     /**
118      * Returns a string that specifies the name of the L&F class
119      * that renders this component.
120      *
121      * @return the string "ButtonUI"
122      * @see JComponent#getUIClassID
123      * @see UIDefaults#getUI
124      * @beaninfo
125      * expert: true
126      * description: A string that specifies the name of the L&F class.
127      */

128     public String JavaDoc getUIClassID() {
129         return uiClassID;
130     }
131
132
133     /**
134      * Gets the value of the <code>defaultButton</code> property,
135      * which if <code>true</code> means that this button is the current
136      * default button for its <code>JRootPane</code>.
137      * Most look and feels render the default button
138      * differently, and may potentially provide bindings
139      * to access the default button.
140      *
141      * @return the value of the <code>defaultButton</code> property
142      * @see JRootPane#setDefaultButton
143      * @see #isDefaultCapable
144      * @beaninfo
145      * description: Whether or not this button is the default button
146      */

147     public boolean isDefaultButton() {
148         JRootPane JavaDoc root = SwingUtilities.getRootPane(this);
149         if (root != null) {
150             return root.getDefaultButton() == this;
151         }
152         return false;
153     }
154
155     /**
156      * Gets the value of the <code>defaultCapable</code> property.
157      *
158      * @return the value of the <code>defaultCapable</code> property
159      * @see #setDefaultCapable
160      * @see #isDefaultButton
161      * @see JRootPane#setDefaultButton
162      */

163     public boolean isDefaultCapable() {
164         return defaultCapable;
165     }
166
167     /**
168      * Sets the <code>defaultCapable</code> property,
169      * which determines whether this button can be
170      * made the default button for its root pane.
171      * The default value of the <code>defaultCapable</code>
172      * property is <code>true</code> unless otherwise
173      * specified by the look and feel.
174      *
175      * @param defaultCapable <code>true</code> if this button will be
176      * capable of being the default button on the
177      * <code>RootPane</code>; otherwise <code>false</code>
178      * @see #isDefaultCapable
179      * @beaninfo
180      * bound: true
181      * attribute: visualUpdate true
182      * description: Whether or not this button can be the default button
183      */

184     public void setDefaultCapable(boolean defaultCapable) {
185         boolean oldDefaultCapable = this.defaultCapable;
186         this.defaultCapable = defaultCapable;
187         firePropertyChange("defaultCapable", oldDefaultCapable, defaultCapable);
188     }
189
190     /**
191      * Overrides <code>JComponent.removeNotify</code> to check if
192      * this button is currently set as the default button on the
193      * <code>RootPane</code>, and if so, sets the <code>RootPane</code>'s
194      * default button to <code>null</code> to ensure the
195      * <code>RootPane</code> doesn't hold onto an invalid button reference.
196      */

197     public void removeNotify() {
198         JRootPane JavaDoc root = SwingUtilities.getRootPane(this);
199         if (root != null && root.getDefaultButton() == this) {
200             root.setDefaultButton(null);
201         }
202         super.removeNotify();
203     }
204
205     /**
206      * Factory method which sets the <code>AbstractButton</code>'s properties
207      * according to values from the <code>Action</code> instance.
208      * The properties which get set may differ for <code>AbstractButton</code>
209      * subclasses. By default, the properties which get set are
210      * <code>Text, Icon, Enabled, ToolTipText, ActionCommand</code>, and
211      * <code>Mnemonic</code>.
212      *
213      * @param a the <code>Action</code> from which to get the
214      * properties, or <code>null</code>
215      * @since 1.3
216      * @see Action
217      * @see #setAction
218      */

219     protected void configurePropertiesFromAction(Action JavaDoc a) {
220         super.configurePropertiesFromAction(a);
221     }
222
223     /**
224      * See readObject() and writeObject() in JComponent for more
225      * information about serialization in Swing.
226      */

227     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
228         s.defaultWriteObject();
229         if (getUIClassID().equals(uiClassID)) {
230             byte count = JComponent.getWriteObjCounter(this);
231             JComponent.setWriteObjCounter(this, --count);
232             if (count == 0 && ui != null) {
233                 ui.installUI(this);
234             }
235         }
236     }
237
238
239     /**
240      * Returns a string representation of this <code>JButton</code>.
241      * This method is intended to be used only for debugging purposes, and the
242      * content and format of the returned string may vary between
243      * implementations. The returned string may be empty but may not
244      * be <code>null</code>.
245      *
246      * @return a string representation of this <code>JButton</code>
247      */

248     protected String JavaDoc paramString() {
249     String JavaDoc defaultCapableString = (defaultCapable ? "true" : "false");
250     
251     return super.paramString() +
252         ",defaultCapable=" + defaultCapableString;
253     }
254
255
256 /////////////////
257
// Accessibility support
258
////////////////
259

260     /**
261      * Gets the <code>AccessibleContext</code> associated with this
262      * <code>JButton</code>. For <code>JButton</code>s,
263      * the <code>AccessibleContext</code> takes the form of an
264      * <code>AccessibleJButton</code>.
265      * A new <code>AccessibleJButton</code> instance is created if necessary.
266      *
267      * @return an <code>AccessibleJButton</code> that serves as the
268      * <code>AccessibleContext</code> of this <code>JButton</code>
269      * @beaninfo
270      * expert: true
271      * description: The AccessibleContext associated with this Button.
272      */

273     public AccessibleContext getAccessibleContext() {
274         if (accessibleContext == null) {
275             accessibleContext = new AccessibleJButton();
276         }
277         return accessibleContext;
278     }
279
280     /**
281      * This class implements accessibility support for the
282      * <code>JButton</code> class. It provides an implementation of the
283      * Java Accessibility API appropriate to button user-interface
284      * elements.
285      * <p>
286      * <strong>Warning:</strong>
287      * Serialized objects of this class will not be compatible with
288      * future Swing releases. The current serialization support is
289      * appropriate for short term storage or RMI between applications running
290      * the same version of Swing. As of 1.4, support for long term storage
291      * of all JavaBeans<sup><font size="-2">TM</font></sup>
292      * has been added to the <code>java.beans</code> package.
293      * Please see {@link java.beans.XMLEncoder}.
294      */

295     protected class AccessibleJButton extends AccessibleAbstractButton {
296     
297         /**
298          * Get the role of this object.
299          *
300          * @return an instance of AccessibleRole describing the role of the
301          * object
302          * @see AccessibleRole
303          */

304         public AccessibleRole getAccessibleRole() {
305             return AccessibleRole.PUSH_BUTTON;
306         }
307     } // inner class AccessibleJButton
308
}
309
Popular Tags