home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / JRadioButton.java < prev    next >
Text File  |  1998-02-26  |  6KB  |  200 lines

  1. /*
  2.  * @(#)JRadioButton.java    1.40 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 disclose
  8.  * such Confidential Information and shall use it only in accordance with the
  9.  * terms of the license agreement you entered into with Sun.
  10.  * 
  11.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  12.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  14.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  15.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  16.  * ITS DERIVATIVES.
  17.  * 
  18.  */
  19. package com.sun.java.swing;
  20.  
  21. import java.awt.*;
  22. import java.awt.event.*;
  23. import com.sun.java.swing.plaf.*;
  24. import com.sun.java.swing.plaf.*;
  25. import com.sun.java.accessibility.*;
  26.  
  27. /**
  28.  * An implementation of a radio button -- an item that can be selected or
  29.  * deselected, and which displays its state to the user.
  30.  * Used with <code>ButtonGroup</code>  to create a group of buttons
  31.  * in which only one button at a time can be selected.
  32.  * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/radiobutton.html">How to Use Radio Buttons</a>
  33.  * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  34.  * for further documentation.
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  * 
  43.  * @beaninfo
  44.  *   attribute: isContainer false
  45.  * @see ButtonGroup
  46.  * @see JCheckBox
  47.  * @version 1.40 02/12/98
  48.  * @author Jeff Dinkins
  49.  */
  50. public class JRadioButton extends JToggleButton implements Accessible {
  51.  
  52.     /**
  53.      * Creates an initially unselected radio button
  54.      * with no set text.
  55.      */
  56.     public JRadioButton () {
  57.         this(null, null, false);
  58.     }
  59.      
  60.     /**
  61.      * Creates an initially unselected radio button
  62.      * with the specified image but no text.
  63.      *
  64.      * @param icon  the image that the button should display
  65.      */
  66.     public JRadioButton(Icon icon) {
  67.         this(null, icon, false);
  68.     }
  69.  
  70.     /**
  71.      * Creates a radio button with the specified image
  72.      * and selection state, but no text.
  73.      *   
  74.      * @param icon  the image that the button should display
  75.      * @param selected  if true, the button is initially selected;
  76.      *                  otherwise, the button is initially unselected
  77.      */
  78.     public JRadioButton(Icon icon, boolean selected) {
  79.         this(null, icon, selected);
  80.     }
  81.     
  82.     /**
  83.      * Creates an unselected radio button with the specified text.
  84.      * 
  85.      * @param text  the string displayed on the radio button
  86.      */
  87.     public JRadioButton (String text) {
  88.         this(text, null, false);
  89.     }
  90.  
  91.     /**
  92.      * Creates a radio button with the specified text
  93.      * and selection state.
  94.      * 
  95.      * @param text  the string displayed on the radio button
  96.      * @param selected  if true, the button is initially selected;
  97.      *                  otherwise, the button is initially unselected
  98.      */
  99.     public JRadioButton (String text, boolean selected) {
  100.         this(text, null, selected);
  101.     }
  102.  
  103.     /**
  104.      * Creates a radio button that has the specified text and image,
  105.      * and that is initially unselected.
  106.      *
  107.      * @param text  the string displayed on the radio button 
  108.      * @param icon  the image that the button should display
  109.      */
  110.     public JRadioButton(String text, Icon icon) {
  111.         this(text, icon, false);
  112.     }
  113.  
  114.     /**
  115.      * Creates a radio button that has the specified text, image,
  116.      * and selection state.
  117.      *
  118.      * @param text  the string displayed on the radio button 
  119.      * @param icon  the image that the button should display
  120.      */
  121.     public JRadioButton (String text, Icon icon, boolean selected) {
  122.         super(text, icon, selected);
  123.         setBorderPainted(false);
  124.         setHorizontalAlignment(LEFT);
  125.     }
  126.  
  127.  
  128.     /**
  129.      * Notification from the UIFactory that the L&F
  130.      * has changed. 
  131.      *
  132.      * @see JComponent#updateUI
  133.      */
  134.     public void updateUI() {
  135.         setUI((ButtonUI)UIManager.getUI(this));
  136.     }
  137.     
  138.  
  139.     /**
  140.      * Returns the name of the L&F class
  141.      * that renders this component.
  142.      *
  143.      * @return String "RadioButtonUI"
  144.      * @see JComponent#getUIClassID
  145.      * @see UIDefaults#getUI
  146.      * @beaninfo
  147.      *        expert: true
  148.      *   description: A string that specifies the name of the L&F class.
  149.      */
  150.     public String getUIClassID() {
  151.         return "RadioButtonUI";
  152.     }
  153.  
  154.  
  155. /////////////////
  156. // Accessibility support
  157. ////////////////
  158.  
  159.  
  160.     /**
  161.      * Get the AccessibleContext associated with this JComponent
  162.      *
  163.      * @return the AccessibleContext of this JComponent
  164.      * @beaninfo
  165.      *       expert: true
  166.      *  description: The AccessibleContext associated with this Button
  167.      */
  168.     public AccessibleContext getAccessibleContext() {
  169.         if (accessibleContext == null) {
  170.             accessibleContext = new AccessibleJRadioButton();
  171.         }
  172.         return accessibleContext;
  173.     }
  174.  
  175.     /**
  176.      * The class used to obtain the accessible role for this object.
  177.      * <p>
  178.      * Warning: serialized objects of this class will not be compatible with
  179.      * future swing releases.  The current serialization support is appropriate
  180.      * for short term storage or RMI between Swing1.0 applications.  It will
  181.      * not be possible to load serialized Swing1.0 objects with future releases
  182.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  183.      * baseline for the serialized form of Swing objects.
  184.      */
  185.     protected class AccessibleJRadioButton extends AccessibleJToggleButton {
  186.  
  187.         /**
  188.          * Get the role of this object.
  189.          *
  190.          * @return an instance of AccessibleRole describing the role of the object
  191.          * @see AccessibleRole
  192.          */
  193.         public AccessibleRole getAccessibleRole() {
  194.             return AccessibleRole.RADIO_BUTTON;
  195.         }
  196.  
  197.     } // inner class AccessibleJRadioButton
  198. }
  199.   
  200.