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