1. /*
  2. * @(#)JButton.java 1.74 01/11/29
  3. *
  4. * Copyright 2002 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. * <p>
  21. * To create a set of mutually exclusive buttons, create a {@link ButtonGroup} object and
  22. * use its <code>add</code> method to include the JButton objects in the group.
  23. * <p>
  24. * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/button.html">How to Use Buttons</a>
  25. * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  26. * for further documentation.
  27. * <p>
  28. * For the keyboard keys used by this component in the standard Look and
  29. * Feel (L&F) renditions, see the
  30. * <a href="doc-files/Key-Index.html#JButton">JButton</a> key assignments.
  31. * <p>
  32. * <strong>Warning:</strong>
  33. * Serialized objects of this class will not be compatible with
  34. * future Swing releases. The current serialization support is appropriate
  35. * for short term storage or RMI between applications running the same
  36. * version of Swing. A future release of Swing will provide support for
  37. * long term persistence.
  38. *
  39. * @beaninfo
  40. * attribute: isContainer false
  41. *
  42. * @version 1.74 11/29/01
  43. * @author Jeff Dinkins
  44. * @see ButtonGroup
  45. */
  46. public class JButton extends AbstractButton implements Accessible {
  47. /**
  48. * @see #getUIClassID
  49. * @see #readObject
  50. */
  51. private static final String uiClassID = "ButtonUI";
  52. private boolean defaultCapable = true;
  53. /**
  54. * Creates a button with no set text or icon.
  55. */
  56. public JButton() {
  57. this(null, null);
  58. }
  59. /**
  60. * Creates a button with an icon.
  61. *
  62. * @param icon the Icon image to display on the button
  63. */
  64. public JButton(Icon icon) {
  65. this(null, icon);
  66. }
  67. /**
  68. * Creates a button with text.
  69. *
  70. * @param text the text of the button
  71. */
  72. public JButton(String text) {
  73. this(text, null);
  74. }
  75. /**
  76. * Creates a button with initial text and an icon.
  77. *
  78. * @param text the text of the button.
  79. * @param icon the Icon image to display on the button
  80. */
  81. public JButton(String text, Icon icon) {
  82. // Create the model
  83. setModel(new DefaultButtonModel());
  84. // initialize
  85. init(text, icon);
  86. }
  87. /**
  88. * Notification from the UIFactory that the L&F
  89. * has changed.
  90. *
  91. * @see JComponent#updateUI
  92. */
  93. public void updateUI() {
  94. setUI((ButtonUI)UIManager.getUI(this));
  95. }
  96. /**
  97. * Returns a string that specifies the name of the L&F class
  98. * that renders this component.
  99. *
  100. * @return "ButtonUI"
  101. * @see JComponent#getUIClassID
  102. * @see UIDefaults#getUI
  103. * @beaninfo
  104. * expert: true
  105. * description: A string that specifies the name of the L&F class.
  106. */
  107. public String getUIClassID() {
  108. return uiClassID;
  109. }
  110. /**
  111. * Returns whether or not this button is the default button
  112. * on the RootPane.
  113. *
  114. * @return "boolean"
  115. * @see JRootPane#setDefaultButton
  116. * @beaninfo
  117. * description: Whether or not this button is the default button
  118. */
  119. public boolean isDefaultButton() {
  120. JRootPane root = SwingUtilities.getRootPane(this);
  121. if (root != null) {
  122. return root.getDefaultButton() == this;
  123. }
  124. return false;
  125. }
  126. /**
  127. * Returns whether or not this button is capable of being
  128. * the default button on the RootPane.
  129. *
  130. * @return "boolean"
  131. * @see #setDefaultCapable
  132. * @see #isDefaultButton
  133. * @see JRootPane#setDefaultButton
  134. */
  135. public boolean isDefaultCapable() {
  136. return defaultCapable;
  137. }
  138. /**
  139. * Sets whether or not this button is capable of being
  140. * the default button on the RootPane.
  141. *
  142. * @return "boolean"
  143. * @see #isDefaultCapable
  144. * @beaninfo
  145. * bound: true
  146. * attribute: visualUpdate true
  147. * description: Whether or not this button can be the default button
  148. */
  149. public void setDefaultCapable(boolean defaultCapable) {
  150. boolean oldDefaultCapable = this.defaultCapable;
  151. this.defaultCapable = defaultCapable;
  152. firePropertyChange("defaultCapable", oldDefaultCapable, defaultCapable);
  153. }
  154. /**
  155. * See readObject() and writeObject() in JComponent for more
  156. * information about serialization in Swing.
  157. */
  158. private void writeObject(ObjectOutputStream s) throws IOException {
  159. s.defaultWriteObject();
  160. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  161. ui.installUI(this);
  162. }
  163. }
  164. /**
  165. * Returns a string representation of this JButton. This method
  166. * is intended to be used only for debugging purposes, and the
  167. * content and format of the returned string may vary between
  168. * implementations. The returned string may be empty but may not
  169. * be <code>null</code>.
  170. *
  171. * @return a string representation of this JButton.
  172. */
  173. protected String paramString() {
  174. String defaultCapableString = (defaultCapable ? "true" : "false");
  175. return super.paramString() +
  176. ",defaultCapable=" + defaultCapableString;
  177. }
  178. /////////////////
  179. // Accessibility support
  180. ////////////////
  181. /**
  182. * Get the AccessibleContext associated with this JComponent
  183. *
  184. * @return the AccessibleContext of this JComponent
  185. * @beaninfo
  186. * expert: true
  187. * description: The AccessibleContext associated with this Button.
  188. */
  189. public AccessibleContext getAccessibleContext() {
  190. if (accessibleContext == null) {
  191. accessibleContext = new AccessibleJButton();
  192. }
  193. return accessibleContext;
  194. }
  195. /**
  196. * The class used to obtain the accessible role for this object.
  197. * <p>
  198. * <strong>Warning:</strong>
  199. * Serialized objects of this class will not be compatible with
  200. * future Swing releases. The current serialization support is appropriate
  201. * for short term storage or RMI between applications running the same
  202. * version of Swing. A future release of Swing will provide support for
  203. * long term persistence.
  204. */
  205. protected class AccessibleJButton extends AccessibleAbstractButton {
  206. /**
  207. * Get the role of this object.
  208. *
  209. * @return an instance of AccessibleRole describing the role of the
  210. * object
  211. * @see AccessibleRole
  212. */
  213. public AccessibleRole getAccessibleRole() {
  214. return AccessibleRole.PUSH_BUTTON;
  215. }
  216. } // inner class AccessibleJButton
  217. }