home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / swing / JButton.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.4 KB  |  179 lines

  1. /*
  2.  * @(#)JButton.java    1.56 98/02/12
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package java.awt.swing;
  21.  
  22. import java.util.EventListener;
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import java.awt.image.*;
  26. import java.awt.swing.plaf.*;
  27. import java.awt.swing.event.*;
  28. import java.awt.accessibility.*;
  29.  
  30. /**
  31.  * An implementation of a "push" button.
  32.  * <p>
  33.  * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/button.html">How to Use Buttons</a>
  34.  * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  35.  * for further documentation.
  36.  * <p>
  37.  * Warning: serialized objects of this class will not be compatible with
  38.  * future swing releases.  The current serialization support is appropriate 
  39.  * for short term storage or RMI between Swing1.0 applications.  It will
  40.  * not be possible to load serialized Swing1.0 objects with future releases
  41.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  42.  * baseline for the serialized form of Swing objects.
  43.  *
  44.  * @beaninfo
  45.  *   attribute: isContainer false
  46.  *
  47.  * @version 1.56 02/12/98
  48.  * @author Jeff Dinkins
  49.  */
  50. public class JButton extends AbstractButton implements Accessible {
  51.  
  52.     /**
  53.      * Creates a button with no set text or icon.
  54.      */
  55.     public JButton() {
  56.         this(null, null);
  57.     }
  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.     /**
  69.      * Creates a button with text.
  70.      *
  71.      * @param text  the text of the button
  72.      */
  73.     public JButton(String text) {
  74.         this(text, null);
  75.     }
  76.     
  77.     /**
  78.      * Creates a button with initial text and an icon.
  79.      *
  80.      * @param text  the text of the button.
  81.      * @param icon  the Icon image to display on the button
  82.      */
  83.     public JButton(String text, Icon icon) {
  84.         // Create the model
  85.         setModel(new DefaultButtonModel());
  86.  
  87.         // initialize
  88.         init(text, icon);
  89.     }
  90.  
  91.     /**
  92.      * Notification from the UIFactory that the L&F
  93.      * has changed. 
  94.      *
  95.      * @see JComponent#updateUI
  96.      */
  97.     public void updateUI() {
  98.         setUI((ButtonUI)UIManager.getUI(this));
  99.     }
  100.     
  101.  
  102.     /**
  103.      * Returns a string that specifies the name of the L&F class
  104.      * that renders this component.
  105.      *
  106.      * @return "ButtonUI"
  107.      * @see JComponent#getUIClassID
  108.      * @see UIDefaults#getUI
  109.      * @beaninfo
  110.      *        expert: true
  111.      *   description: A string that specifies the name of the L&F class.
  112.      */
  113.     public String getUIClassID() {
  114.         return "ButtonUI";
  115.     }
  116.  
  117.  
  118.     /**
  119.      * Returns whether or not this button is the default button
  120.      * on the RootPane.
  121.      *
  122.      * @return "boolean"
  123.      * @see JRootPane#setDefaultButton
  124.      * @beaninfo 
  125.      *  description: Whether or not this button is the default button
  126.      */
  127.     public boolean isDefaultButton() {
  128.         JRootPane root = SwingUtilities.getRootPane(this);
  129.         if (root != null) {
  130.             return root.getDefaultButton() == this;
  131.         }
  132.         return false;
  133.     }
  134.  
  135.  
  136. /////////////////
  137. // Accessibility support
  138. ////////////////
  139.  
  140.     /**
  141.      * Get the AccessibleContext associated with this JComponent
  142.      *
  143.      * @return the AccessibleContext of this JComponent
  144.      * @beaninfo
  145.      *       expert: true
  146.      *  description: The AccessibleContext associated with this Button.
  147.      */
  148.     public AccessibleContext getAccessibleContext() {
  149.         if (accessibleContext == null) {
  150.             accessibleContext = new AccessibleJButton();
  151.         }
  152.         return accessibleContext;
  153.     }
  154.  
  155.     /**
  156.      * The class used to obtain the accessible role for this object.
  157.      * <p>
  158.      * Warning: serialized objects of this class will not be compatible with
  159.      * future swing releases.  The current serialization support is appropriate
  160.      * for short term storage or RMI between Swing1.0 applications.  It will
  161.      * not be possible to load serialized Swing1.0 objects with future releases
  162.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  163.      * baseline for the serialized form of Swing objects.
  164.      */
  165.     protected class AccessibleJButton extends AccessibleAbstractButton {
  166.     
  167.         /**
  168.          * Get the role of this object.
  169.          *
  170.          * @return an instance of AccessibleRole describing the role of the 
  171.          * object
  172.          * @see AccessibleRole
  173.          */
  174.         public AccessibleRole getAccessibleRole() {
  175.             return AccessibleRole.PUSH_BUTTON;
  176.         }
  177.     } // inner class AccessibleJButton
  178. }
  179.