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

  1. /*
  2.  * @(#)Label.java    1.30 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.LabelPeer;
  17.  
  18. /**
  19.  * A <code>Label</code> object is a component for placing text in a 
  20.  * container. A label displays a single line of read-only text.
  21.  * The text can be changed by the application, but a user cannot edit it 
  22.  * directly.  
  23.  * <p>
  24.  * For example, the code . . .
  25.  * <p>
  26.  * <hr><blockquote><pre>
  27.  * setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); 
  28.  * add(new Label("Hi There!")); 
  29.  * add(new Label("Another Label"));
  30.  * </pre></blockquote><hr>
  31.  * <p>
  32.  * produces the following label:
  33.  * <p>
  34.  * <img src="images-awt/Label-1.gif" 
  35.  * ALIGN=center HSPACE=10 VSPACE=7>
  36.  *
  37.  * @version    1.30, 03/18/98
  38.  * @author     Sami Shaio
  39.  * @since       JDK1.0
  40.  */
  41. public class Label extends Component {
  42.  
  43.     static {
  44.         Toolkit.loadLibraries();
  45.         initIDs();
  46.     }
  47.  
  48.     /**
  49.      * Indicates that the label should be left justified. 
  50.      */
  51.     public static final int LEFT     = 0;
  52.  
  53.     /** 
  54.      * Indicates that the label should be centered. 
  55.      */
  56.     public static final int CENTER     = 1;
  57.  
  58.     /**
  59.      * Indicates that the label should be right justified. 
  60.      * @since   JDK1.0t.
  61.      */
  62.     public static final int RIGHT     = 2;
  63.  
  64.     /**
  65.      * The text of this label.
  66.      */
  67.     String text;
  68.     
  69.     /**
  70.      * The label's alignment.  The default alignment is set
  71.      * to be left justified.
  72.      */
  73.     int       alignment = LEFT;
  74.  
  75.     private static final String base = "label";
  76.     private static int nameCounter = 0;
  77.  
  78.     /*
  79.      * JDK 1.1 serialVersionUID 
  80.      */
  81.      private static final long serialVersionUID = 3094126758329070636L;
  82.  
  83.     /**
  84.      * Constructs an empty label.
  85.      */
  86.     public Label() {
  87.     this("", LEFT);
  88.     }
  89.  
  90.     /**
  91.      * Constructs a new label with the specified string of text, 
  92.      * left justified.
  93.      * @param text the string that the label presents.
  94.      */
  95.     public Label(String text) {
  96.         this(text, LEFT);
  97.     }
  98.  
  99.     /**
  100.      * Constructs a new label that presents the specified string of 
  101.      * text with the specified alignment.
  102.      * <p>
  103.      * Possible values for <code>alignment</code> are <code>Label.LEFT</code>, 
  104.      * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  105.      * @param     text        the string that the label presents.
  106.      * @param     alignment   the alignment value.
  107.      */
  108.     public Label(String text, int alignment) {
  109.     this.name = base + nameCounter++;
  110.     this.text = text;
  111.     setAlignment(alignment);
  112.     }
  113.  
  114.     /**
  115.      * Creates the peer for this label.  The peer allows us to
  116.      * modify the appearance of the label without changing its 
  117.      * functionality.
  118.      */
  119.     public void addNotify() {
  120.     peer = getToolkit().createLabel(this);
  121.     super.addNotify();
  122.     }
  123.  
  124.     /** 
  125.      * Gets the current alignment of this label. Possible values are
  126.      * <code>Label.LEFT</code>, <code>Label.RIGHT</code>, and 
  127.      * <code>Label.CENTER</code>.
  128.      * @see        java.awt.Label#setAlignment
  129.      */
  130.     public int getAlignment() {
  131.     return alignment;
  132.     }
  133.  
  134.     /** 
  135.      * Sets the alignment for this label to the specified alignment.
  136.      * Possible values are <code>Label.LEFT</code>, 
  137.      * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  138.      * @param      alignment    the alignment to be set.
  139.      * @exception  IllegalArgumentException if an improper value for 
  140.      *                          <code>alignment</code> is given. 
  141.      * @see        java.awt.Label#getAlignment
  142.      */
  143.     public synchronized void setAlignment(int alignment) {
  144.     switch (alignment) {
  145.       case LEFT:
  146.       case CENTER:
  147.       case RIGHT:
  148.         this.alignment = alignment;
  149.             LabelPeer peer = (LabelPeer)this.peer;
  150.         if (peer != null) {
  151.         peer.setAlignment(alignment);
  152.         }
  153.         return;
  154.     }
  155.     throw new IllegalArgumentException("improper alignment: " + alignment);
  156.     }
  157.  
  158.     /** 
  159.      * Gets the text of this label. 
  160.      * @return     the text of this label.
  161.      * @see        java.awt.Label#setText
  162.      */
  163.     public String getText() {
  164.     return text;
  165.     }
  166.  
  167.     /** 
  168.      * Sets the text for this label to the specified text.
  169.      * @param      text the text that this label presents. 
  170.      * @see        java.awt.Label#getText
  171.      */
  172.     public synchronized void setText(String text) {
  173.     if (text != this.text && (this.text == null
  174.                     || !this.text.equals(text))) {
  175.         this.text = text;
  176.             LabelPeer peer = (LabelPeer)this.peer;
  177.         if (peer != null) {
  178.         peer.setText(text);
  179.         }
  180.     }
  181.     }
  182.  
  183.     /**
  184.      * Returns the parameter string representing the state of this 
  185.      * label. This string is useful for debugging. 
  186.      * @return     the parameter string of this label.
  187.      */
  188.     protected String paramString() {
  189.     String str = ",align=";
  190.     switch (alignment) {
  191.       case LEFT:   str += "left"; break;
  192.       case CENTER: str += "center"; break;
  193.       case RIGHT:  str += "right"; break;
  194.     }
  195.     return super.paramString() + str + ",text=" + text;
  196.     }
  197.  
  198.     /**
  199.      * Initialize JNI field and method IDs
  200.      */
  201.     private static native void initIDs();
  202. }
  203.