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