TextField object is a text component
that allows for the editing of a single line of text.
For example, the following image depicts a frame with four
text fields of varying widths. Two of these text fields
display the predefined text "Hello".
Here is the code that produces these four text fields:
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);
Every time the user types a key in the text field, one or
more key events are sent to the text field. A KeyEvent
may be one of three types: keyPressed, keyReleased, or keyTyped.
The properties of a key event indicate which of these types
it is, as well as additional information about the event,
such as what modifiers are applied to the key event and the
time at which the event occurred.
The key event is passed to every KeyListener
or KeyAdapter object which registered to receive such
events using the component's addKeyListener method.
(KeyAdapter objects implement the
KeyListener interface.)
It is also possible to fire an ActionEvent.
If action events are enabled for the text field, they may
be fired by pressing the Return key.
The TextField class's processEvent
method examines the action event and passes it along to
processActionEvent. The latter method redirects the
event to any ActionListener objects that have
registered to receive action events generated by this
text field.
java.awt.event.KeyEvent , java.awt.event.KeyAdapter , java.awt.event.KeyListener , java.awt.event.ActionEvent , java.awt.Component#addKeyListener , java.awt.TextField#processEvent , java.awt.TextField#processActionEvent , java.awt.TextField#addActionListener
| Modifier and Type | Class and Description |
|---|---|
| protected class | TextField.
This class implements accessibility support for the
|
| Modifier and Type | Field and Description |
|---|---|
| pack-priv transient ActionListener | |
| private static final String | |
| pack-priv int | columns
The number of columns in the text field. |
| pack-priv char | echoChar
The echo character, which is used when the user wishes to disguise the characters typed into the text field. |
| private static int | |
| private static final long | serialVersionUID
Use serialVersionUID from JDK 1.1 for interoperability.
|
| private int | textFieldSerializedDataVersion
The textField Serialized Data Version. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public | TextField(int
the number of columns. If
columns)columns is less than 0,
columns is set to 0.Constructs a new empty text field with the specified number of columns. |
| public | TextField(String
the text to be displayed. If
text, int text is null, the empty
string "" will be displayed.
If text contains EOL and/or LF characters, then
each will be replaced by space character.the number of columns. If
columns)columns is less than 0,
columns is set to 0.Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the specified number of columns. |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized void | addActionListener(ActionListener
the action listener. l)Adds the specified action listener to receive action events from this text field. |
| public void | |
| pack-priv String | constructComponentName()
Overrides java. Construct a name for this component. |
| public boolean | Returns: true if this text field has
a character set for echoing;
false otherwise.Indicates whether or not this text field has a character set for echoing. |
| pack-priv boolean | |
| public AccessibleContext | Returns: an AccessibleAWTTextField that serves as the AccessibleContext of this TextFieldOverrides java. Implements javax. Gets the AccessibleContext associated with this TextField. |
| public synchronized ActionListener[] | Returns: all of this textfield'sActionListeners
or an empty array if no action
listeners are currently registeredReturns an array of all the action listeners registered on this textfield. |
| public int | |
| public char | Returns: the echo character for this text field.Gets the character that is to be used for echoing. |
| public <T extends EventListener> T[] | Returns: an array of all objects registered asFooListeners on this textfield,
or an empty array if no such
listeners have been addedthe type of listeners requested; this parameter
should specify an interface that descends from
listenerType)java.util.EventListenerOverrides java. Returns an array of all the objects currently registered
as |
| public Dimension | Returns: the minimum size for this text fieldthe number of columns in
this text field. columns)Gets the minimum dimensions for a text field with the specified number of columns. |
| public Dimension | Returns: the minimum dimensions for displaying this text field.Overrides java. Gets the minimum dimensions for this text field. |
| public Dimension | Returns: the preferred dimensions for displaying this text field.the number of columns
in this text field. columns)Gets the preferred size of this text field with the specified number of columns. |
| public Dimension | Returns: the preferred dimensions for displaying this text field.Overrides java. Gets the preferred size of this text field. |
| public Dimension | Returns: the minimum size for this text fieldthe number of columns columns)
Deprecated
As of JDK version 1.1,
replaced by
getMinimumSize(int).
Returns the minimum dimensions for a text field with the specified number of columns. |
| public Dimension | minimumSize()
Overrides java.
Deprecated
As of JDK version 1.1,
replaced by
getMinimumSize().
Returns the minimum size of this component. |
| protected String | Returns: the parameter string of this text fieldOverrides java. Returns a string representing the state of this |
| public Dimension | Returns: the preferred size for the text fieldthe number of columns columns)
Deprecated
As of JDK version 1.1,
replaced by
getPreferredSize(int).
Returns the preferred size for this text field with the specified number of columns. |
| public Dimension | preferredSize()
Overrides java.
Deprecated
As of JDK version 1.1,
replaced by
getPreferredSize().
Returns the component's preferred size. |
| protected void | processActionEvent(ActionEvent
the action event e)Processes action events occurring on this text field by
dispatching them to any registered
|
| protected void | processEvent(AWTEvent
the event e)Overrides java. Processes events on this text field. |
| private void | readObject(ObjectInputStream
the s)ObjectInputStream to readHides java. Read the ObjectInputStream and if it isn't null, add a listener to receive action events fired by the TextField. |
| public synchronized void | removeActionListener(ActionListener
the action listener. l)Removes the specified action listener so that it no longer receives action events from this text field. |
| private static String | Returns: Returns text after replacing EOL characters.the new text. text)Replaces EOL characters from the text variable with a space character. |
| public void | |
| public void | setEchoChar(char
the echo character for this text field. c)Sets the echo character for this text field. |
| public synchronized void | setEchoCharacter(char
the echo character for this text field c)
Deprecated
As of JDK version 1.1,
replaced by
setEchoChar(char).
Sets the character to be echoed when protected input is displayed. |
| public void | setText(String
the new text. If
t)t is null, the empty
string "" will be displayed.
If t contains EOL and/or LF characters, then
each will be replaced by space character.Overrides java. Sets the text that is presented by this text component to be the specified text. |
| private void | writeObject(ObjectOutputStream
the s)ObjectOutputStream to writeHides java. Writes default serializable fields to stream. |
| actionListener | back to summary |
|---|---|
| pack-priv transient ActionListener actionListener | |
| base | back to summary |
|---|---|
| private static final String base | |
| columns | back to summary |
|---|---|
| pack-priv int columns The number of columns in the text field. A column is an approximate average character width that is platform-dependent. Guaranteed to be non-negative.
| |
| echoChar | back to summary |
|---|---|
| pack-priv char echoChar The echo character, which is used when
the user wishes to disguise the characters
typed into the text field.
The disguises are removed if echoChar =
| |
| nameCounter | back to summary |
|---|---|
| private static int nameCounter | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides java. Use serialVersionUID from JDK 1.1 for interoperability. | |
| textFieldSerializedDataVersion | back to summary |
|---|---|
| private int textFieldSerializedDataVersion The textField Serialized Data Version. | |
| TextField | back to summary |
|---|---|
| public TextField() throws HeadlessException Constructs a new text field.
| |
| TextField | back to summary |
|---|---|
| public TextField(String text) throws HeadlessException Constructs a new text field initialized with the specified text.
| |
| TextField | back to summary |
|---|---|
| public TextField(int columns) throws HeadlessException Constructs a new empty text field with the specified number of columns. A column is an approximate average character width that is platform-dependent.
| |
| TextField | back to summary |
|---|---|
| public TextField(String text, int columns) throws HeadlessException Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the specified number of columns. A column is an approximate average character width that is platform-dependent.
| |
| addActionListener | back to summary |
|---|---|
| public synchronized void addActionListener(ActionListener l) Adds the specified action listener to receive action events from this text field. If l is null, no exception is thrown and no action is performed. Refer to AWT Threading Issues for details on AWT's threading model.
| |
| addNotify | back to summary |
|---|---|
| public void addNotify() Overrides java. Creates the TextField's peer. The peer allows us to modify the appearance of the TextField without changing its functionality. | |
| constructComponentName | back to summary |
|---|---|
| pack-priv String constructComponentName() Overrides java. Construct a name for this component. Called by getName() when the name is null. | |
| echoCharIsSet | back to summary |
|---|---|
| public boolean echoCharIsSet() Indicates whether or not this text field has a character set for echoing. An echo character is useful for text fields where user input should not be echoed to the screen, as in the case of a text field for entering a password.
| |
| eventEnabled | back to summary |
|---|---|
| pack-priv boolean eventEnabled(AWTEvent e) Overrides java. | |
| getAccessibleContext | back to summary |
|---|---|
| public AccessibleContext getAccessibleContext() Overrides java. Implements javax. Gets the AccessibleContext associated with this TextField. For text fields, the AccessibleContext takes the form of an AccessibleAWTTextField. A new AccessibleAWTTextField instance is created if necessary.
| |
| getActionListeners | back to summary |
|---|---|
| public synchronized ActionListener[] getActionListeners() Returns an array of all the action listeners registered on this textfield.
| |
| getColumns | back to summary |
|---|---|
| public int getColumns() Gets the number of columns in this text field. A column is an approximate average character width that is platform-dependent.
| |
| getEchoChar | back to summary |
|---|---|
| public char getEchoChar() Gets the character that is to be used for echoing.
An echo character is useful for text fields where
user input should not be echoed to the screen, as in
the case of a text field for entering a password.
If A Java platform implementation may support only a limited, non-empty set of echo characters. This function returns the echo character originally requested via setEchoChar(). The echo character actually used by the TextField implementation might be different.
| |
| getListeners | back to summary |
|---|---|
| public <T extends EventListener> T[] getListeners(Class<T> listenerType) Overrides java. Returns an array of all the objects currently registered
as
You can specify the ActionListener[] als = (ActionListener[])(t.getListeners(ActionListener.class));If no such listeners exist, this method returns an empty array.
| |
| getMinimumSize | back to summary |
|---|---|
| public Dimension getMinimumSize(int columns) Gets the minimum dimensions for a text field with the specified number of columns.
| |
| getMinimumSize | back to summary |
|---|---|
| public Dimension getMinimumSize() Overrides java. Gets the minimum dimensions for this text field.
| |
| getPreferredSize | back to summary |
|---|---|
| public Dimension getPreferredSize(int columns) Gets the preferred size of this text field with the specified number of columns.
| |
| getPreferredSize | back to summary |
|---|---|
| public Dimension getPreferredSize() Overrides java. Gets the preferred size of this text field.
| |
| minimumSize | back to summary |
|---|---|
| public Dimension minimumSize(int columns)
Deprecated As of JDK version 1.1,
replaced by Returns the minimum dimensions for a text field with the specified number of columns.
| |
| minimumSize | back to summary |
|---|---|
| public Dimension minimumSize() Overrides java. Doc from java. Deprecated As of JDK version 1.1,
replaced by Returns the minimum size of this component.
| |
| paramString | back to summary |
|---|---|
| protected String paramString() Overrides java. Returns a string representing the state of this
| |
| preferredSize | back to summary |
|---|---|
| public Dimension preferredSize(int columns)
Deprecated As of JDK version 1.1,
replaced by Returns the preferred size for this text field with the specified number of columns.
| |
| preferredSize | back to summary |
|---|---|
| public Dimension preferredSize() Overrides java. Doc from java. Deprecated As of JDK version 1.1,
replaced by Returns the component's preferred size.
| |
| processActionEvent | back to summary |
|---|---|
| protected void processActionEvent(ActionEvent e) Processes action events occurring on this text field by
dispatching them to any registered
This method is not called unless action events are enabled for this component. Action events are enabled when one of the following occurs:
Note that if the event parameter is
| |
| processEvent | back to summary |
|---|---|
| protected void processEvent(AWTEvent e) Overrides java. Processes events on this text field. If the event
is an instance of Note that if the event parameter is
| |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException, HeadlessException Hides java. Read the ObjectInputStream and if it isn't null, add a listener to receive action events fired by the TextField. Unrecognized keys or values will be ignored.
| |
| removeActionListener | back to summary |
|---|---|
| public synchronized void removeActionListener(ActionListener l) Removes the specified action listener so that it no longer receives action events from this text field. If l is null, no exception is thrown and no action is performed. Refer to AWT Threading Issues for details on AWT's threading model.
| |
| replaceEOL | back to summary |
|---|---|
| private static String replaceEOL(String text) Replaces EOL characters from the text variable with a space character. | |
| setColumns | back to summary |
|---|---|
| public void setColumns(int columns) Sets the number of columns in this text field. A column is an approximate average character width that is platform-dependent.
| |
| setEchoChar | back to summary |
|---|---|
| public void setEchoChar(char c) Sets the echo character for this text field.
An echo character is useful for text fields where
user input should not be echoed to the screen, as in
the case of a text field for entering a password.
Setting A Java platform implementation may support only a limited, non-empty set of echo characters. Attempts to set an unsupported echo character will cause the default echo character to be used instead. Subsequent calls to getEchoChar() will return the echo character originally requested. This might or might not be identical to the echo character actually used by the TextField implementation.
| |
| setEchoCharacter | back to summary |
|---|---|
| public synchronized void setEchoCharacter(char c)
Deprecated As of JDK version 1.1,
replaced by Sets the character to be echoed when protected input is displayed.
| |
| setText | back to summary |
|---|---|
| public void setText(String t) Overrides java. Sets the text that is presented by this text component to be the specified text.
| |
| writeObject | back to summary |
|---|---|
| private void writeObject(ObjectOutputStream s) throws IOException Hides java. Writes default serializable fields to stream. Writes a list of serializable ActionListener(s) as optional data. The non-serializable ActionListener(s) are detected and no attempt is made to serialize them.
| |
TextField class. It provides an implementation of the
Java Accessibility API appropriate to text field user-interface elements.
| Modifier and Type | Field and Description |
|---|---|
| private static final long | serialVersionUID
Hides java. |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public AccessibleStateSet | Returns: an instance of AccessibleStateSet describing the states of the objectOverrides java. Gets the state set of this object. |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides java. Use serialVersionUID from JDK 1.3 for interoperability. | |
| AccessibleAWTTextField | back to summary |
|---|---|
| protected AccessibleAWTTextField() Constructs an | |
| getAccessibleStateSet | back to summary |
|---|---|
| public AccessibleStateSet getAccessibleStateSet() Overrides java. Gets the state set of this object.
| |