Package java.awt |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
public class java.awt.TextField extends java.awt.TextComponent { // Constructors public TextField(); public TextField(int cols); public TextField(String text); public TextField(String text, int cols); // Methods public void addNotify(); public boolean echoCharIsSet(); public int getColumns(); public char getEchoChar(); public Dimension minimumSize(); public Dimension minimumSize(int cols); protected String paramString(); public Dimension preferredSize(); public Dimension preferredSize(int cols); public void setEchoCharacter(char c); }
A text field is a component that presents the user with a single editable line of text.
For example, the following code code:
TextField tf1, tf2, tf3, tf4; // a blank text field tf1 = new TextField(); // blank field of 20 columns tf2 = new TextField(20); // Predefined text displayed tf3 = new TextField("Hello!"); // Predefined text in 30 columns tf4 = new TextField("Hello", 30);
produces the text fields shown below:
Every time the user types a key(1) in the text field, AWT sends a key press event (KEY_PRESS) and a key release event (KEY_RELEASE) to the text field. These events' target field is the button, and their key field is the key typed.
In addition, whether the user types the return key, AWT sends an action event (ACTION_EVENT) to the text field. This event's target is the text field, and its object is the string contents of the text field; the string will not have a return character as its last character. An application should override the action method of the text field or of one of its containing windows in order to cause some action to occur.
public TextField()Constructs a new text field.
public TextField(int cols)Constructs a new text field the specified number of characters wide.
Parameter Description cols the number of character
public TextField(String text)Constructs a new text field initialized with the specified text.
Parameter Description text the text to be displayed
public TextField(String text, int cols)Constructs a new text field initialized with the specified text and wide enough to hold the specified number of characters.
Parameter Description text the text to be displayed cols the number of characters
public void addNotify()This method calls the createTextField method of this object's toolkit in order to create a TextFieldPeer for this text field. This peer allows the application to change the look of a text field without changing its functionality.
Most applications do not call this method directly.
Overrides:
addNotify in class Component .
public boolean echoCharIsSet()Return Value:
Returns true if this text field has a character set for echoing; false otherwise.
See Also: setEchoCharacter getEchoChar .
public int getColumns()Return Value:
Returns the number of columns in this text field.
public char getEchoChar()Return Value:
Returns the echo character for this text field.
See Also: setEchoCharacter echoCharIsSet .
public Dimension minimumSize()Determines the minimum size of this text field. If the application has specified the number of columns for this text field, and it is greater than zero, then this text field's peer's minimumSize method is called with the number columns in order to determine the minimum size.
If this text field does not have a peer, or if the number of columns specified by the application is less than or equal to zero, the superclass's minimumSize method is called to determine the minimum size.
Return Value:
Returns the minimum dimensions needed for this text area.
Overrides:
minimumSize in class Component .
public Dimension minimumSize(int cols)Determines the minimum size of a text field with the specified number of columns. This text field's peer's minimumSize method is called with the number of columns in order to determine the minimum size.
If this text field does not have a peer the superclass's minimumSize method is called to determine the minimum size.
Return Value:
Returns the minimum dimensions needed to display a text area with the specified number of columns.
Parameter Description cols the number of columns
protected String paramString()Returns the parameter string representing the state of this text field. This string is useful for debugging.
Return Value:
Returns the parameter string of this text field.
Overrides:
paramString in class TextComponent .
public Dimension preferredSize()Determines the preferred size of this text field. If the application has specified the number of columns for this text field, and it is greater than zero, then this text field's peer's preferredSize method is called with the number of columns in order to determine the preferred size.
If this text field does not have a peer, or if the number of columns specified by the application is less than or equal to zero, the superclass's preferredSize method is called to determine the preferred size.
Return Value:
Returns the preferred dimensions needed for this text field.
Overrides:
preferredSize in class Component .
public Dimension preferredSize(int cols)Determines the preferred size of a text field with the specified number of columns. This text field's peer's preferredSize method is called with the number columns in order to determine the preferred size.
If this text field does not have a peer the superclass's preferredSize method is called to determine the preferred size.
Return Value:
Returns the preferred dimensions needed to display the text field with the specified number of columns.
Parameter Description cols the number of columns
public void setEchoCharacter(char c)Sets the echo character for this text field. Any character that the user types in the text field is echoed in this text field as the echo character.
An echo character is useful for fields where the user input shouldn't be echoed to the screen such as in the case of a text field for typing in a password.
Parameter Description c the echo character for this text field See Also: echoCharIsSet getEchoChar .
(1)In Java 1.0, the AWT does not send mouse or focus events to a text area. In Java 1.1, the AWT sends the text area all mouse, keyboard, and focus events that occur over it.