home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / TextField.java < prev    next >
Text File  |  1996-05-03  |  5KB  |  176 lines

  1. /*
  2.  * @(#)TextField.java    1.17 96/04/01 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.awt.peer.TextFieldPeer;
  22.  
  23. /**
  24.  * TextField is a component that allows the editing of a single line of text.
  25.  *
  26.  * @version    1.17, 01 Apr 1996
  27.  * @author     Sami Shaio
  28.  */
  29. public class TextField extends TextComponent {
  30.  
  31.     /**
  32.      * The number of columns in the TextField.
  33.      */
  34.     int cols;
  35.  
  36.     /**
  37.      * The echo character.
  38.      */
  39.     char echoChar;
  40.  
  41.     /**
  42.      * Constructs a new TextField.
  43.      */
  44.     public TextField() {
  45.     super("");
  46.     }
  47.  
  48.     /**
  49.      * Constructs a new TextField initialized with the specified columns.
  50.      * @param cols the number of columns
  51.      */
  52.     public TextField(int cols) {
  53.     super("");
  54.     this.cols = cols;
  55.     }
  56.  
  57.     /**
  58.      * Constructs a new TextField initialized with the specified text.
  59.      * @param text the text to be displayed
  60.      */
  61.     public TextField(String text) {
  62.     super(text);
  63.     }
  64.  
  65.     /**
  66.      * Constructs a new TextField initialized with the specified text and columns.
  67.      * @param text the text to be displayed
  68.      * @param cols the number of columns
  69.      */
  70.     public TextField(String text, int cols) {
  71.     super(text);
  72.     this.cols = cols;
  73.     }
  74.  
  75.     /**
  76.      * Creates the TextField's peer.  The peer allows us to modify the appearance of 
  77.      * the TextField without changing its functionality.
  78.      */
  79.     public synchronized void addNotify() {
  80.     peer = getToolkit().createTextField(this);
  81.     super.addNotify();
  82.     }
  83.  
  84.     /**
  85.      * Returns the character to be used for echoing.
  86.      * @see #setEchoCharacter
  87.      * @see #echoCharIsSet
  88.      */
  89.     public char getEchoChar() {
  90.     return echoChar;
  91.     }
  92.  
  93.     /**
  94.      * Returns true if this TextField has a character set for
  95.      * echoing.
  96.      * @see #setEchoCharacter
  97.      * @see #getEchoChar
  98.      */
  99.     public boolean echoCharIsSet() {
  100.     return echoChar != 0;
  101.     }
  102.  
  103.     /**
  104.      * Returns the number of columns in this TextField.
  105.      */
  106.     public int getColumns() {
  107.     return cols;
  108.     }
  109.  
  110.     /**
  111.      * Sets the echo character for this TextField. This is useful
  112.      * for fields where the user input shouldn't be echoed to the screen,
  113.      * as in the case of a TextField that represents a password.
  114.      * @param c the echo character for this TextField
  115.      * @see #echoCharIsSet
  116.      * @see #getEchoChar
  117.      */
  118.     public void setEchoCharacter(char c) {
  119.     echoChar = c;
  120.     TextFieldPeer peer = (TextFieldPeer)this.peer;
  121.     if (peer != null) {
  122.         peer.setEchoCharacter(c);
  123.     }
  124.     }
  125.  
  126.     /**
  127.      * Returns the preferred size Dimensions needed for this TextField with the 
  128.      * specified amount of columns.
  129.      * @param cols the number of columns in this TextField
  130.      */
  131.     public Dimension preferredSize(int cols) {
  132.     TextFieldPeer peer = (TextFieldPeer)this.peer;
  133.     return (peer != null) ? peer.preferredSize(cols) : super.preferredSize();
  134.     }
  135.  
  136.     /**
  137.      * Returns the preferred size Dimensions needed for this TextField.
  138.      */
  139.     public Dimension preferredSize() {
  140.     return (cols > 0) ? preferredSize(cols) : super.preferredSize();
  141.     }
  142.  
  143.     /**
  144.      * Returns the minimum size Dimensions needed for this TextField with the specified 
  145.      * amount of columns.
  146.      * @param cols the number of columns in this TextField
  147.      */
  148.     public Dimension minimumSize(int cols) {
  149.     TextFieldPeer peer = (TextFieldPeer)this.peer;
  150.     return (peer != null) ? peer.minimumSize(cols) : super.minimumSize();
  151.     }
  152.  
  153.     /**
  154.      * Returns the minimum size Dimensions needed for this TextField.
  155.      */
  156.     public Dimension minimumSize() {
  157.     return (cols > 0) ? minimumSize(cols) : super.minimumSize();
  158.     }
  159.  
  160.     /**
  161.      * Returns the String of parameters for this TExtField.
  162.      */
  163.     protected String paramString() {
  164.     String str = super.paramString();
  165.     if (echoChar != 0) {
  166.         str += ",echo=" + echoChar;
  167.     }
  168.     return str;
  169.     }
  170.  
  171.     /* Can this field be tabbed to? */
  172.     boolean tabbable() {
  173.         return true;
  174.     }
  175. }
  176.