APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.awt

public non-sealed Class TextField

extends TextComponent
Class Inheritance
Imports
java.awt.event.ActionEvent, .ActionListener, java.awt.peer.TextFieldPeer, java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serial, java.util.EventListener, javax.accessibility.AccessibleContext, .AccessibleState, .AccessibleStateSet

A 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".

The preceding text describes this
image.

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.

Author
Sami Shaio
Since
1.0
See Also
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

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
TextField.AccessibleAWTTextField

This class implements accessibility support for the TextField class.

Field Summary

Modifier and TypeField 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

Hides java.awt.TextComponent.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.
private int
textFieldSerializedDataVersion

The textField Serialized Data Version.

Inherited from java.awt.TextComponent:
backgroundSetByClientCodeeditableselectionEndselectionStarttexttextListener

Constructor Summary

AccessConstructor and Description
public
TextField()

Constructs a new text field.

public
TextField(String
the text to be displayed. If 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.
text
)

Constructs a new text field initialized with the specified text.

public
TextField(int
the number of columns. If columns is less than 0, columns is set to 0.
columns
)

Constructs a new empty text field with the specified number of columns.

public
TextField(String
the text to be displayed. If 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.
text
,
int
the number of columns. If columns is less than 0, columns is set to 0.
columns
)

Constructs a new text field initialized with the specified text to be displayed, and wide enough to hold the specified number of columns.

Method Summary

Modifier and TypeMethod 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
addNotify()

Overrides java.awt.TextComponent.addNotify.

Creates the TextField's peer.

pack-priv String
constructComponentName()

Overrides java.awt.Component.constructComponentName.

Construct a name for this component.

public boolean

Returns:

true if this text field has a character set for echoing; false otherwise.
echoCharIsSet
()

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 TextField
getAccessibleContext
()

Overrides java.awt.TextComponent.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this TextField.

public synchronized ActionListener[]

Returns:

all of this textfield's ActionListeners or an empty array if no action listeners are currently registered
getActionListeners
()

Returns an array of all the action listeners registered on this textfield.

public int

Returns:

the number of columns.
getColumns
()

Gets the number of columns in this text field.

public char

Returns:

the echo character for this text field.
getEchoChar
()

Gets the character that is to be used for echoing.

public <T extends EventListener> T[]

Returns:

an array of all objects registered as FooListeners on this textfield, or an empty array if no such listeners have been added
getListeners
(Class<T>
the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
listenerType
)

Overrides java.awt.TextComponent.getListeners.

Returns an array of all the objects currently registered as FooListeners upon this TextField.

public Dimension

Returns:

the minimum size for this text field
getMinimumSize
(int
the 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.
getMinimumSize
()

Overrides java.awt.Component.getMinimumSize.

Gets the minimum dimensions for this text field.

public Dimension

Returns:

the preferred dimensions for displaying this text field.
getPreferredSize
(int
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.
getPreferredSize
()

Overrides java.awt.Component.getPreferredSize.

Gets the preferred size of this text field.

public Dimension

Returns:

the minimum size for this text field
minimumSize
(int
the 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.awt.Component.minimumSize.

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 field
paramString
()

Overrides java.awt.TextComponent.paramString.

Returns a string representing the state of this TextField.

public Dimension

Returns:

the preferred size for the text field
preferredSize
(int
the 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.awt.Component.preferredSize.

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 ActionListener objects.

protected void
processEvent(AWTEvent
the event
e
)

Overrides java.awt.TextComponent.processEvent.

Processes events on this text field.

private void
readObject(ObjectInputStream
the ObjectInputStream to read
s
)

Hides java.awt.TextComponent.readObject.

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.
replaceEOL
(String
the new text.
text
)

Replaces EOL characters from the text variable with a space character.

public void
setColumns(int
the number of columns.
columns
)

Sets the number of columns in this text field.

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 is null, the empty string "" will be displayed. If t contains EOL and/or LF characters, then each will be replaced by space character.
t
)

Overrides java.awt.TextComponent.setText.

Sets the text that is presented by this text component to be the specified text.

private void
writeObject(ObjectOutputStream
the ObjectOutputStream to write
s
)

Hides java.awt.TextComponent.writeObject.

Writes default serializable fields to stream.

Inherited from java.awt.TextComponent:
addTextListenerareInputMethodsEnabledenableInputMethodsgetBackgroundgetCaretPositiongetInputMethodRequestsgetSelectedTextgetSelectionEndgetSelectionStartgetTextgetTextListenersisEditableprocessTextEventremoveNotifyremoveTextListenerselectselectAllsetBackgroundsetCaretPositionsetEditablesetSelectionEndsetSelectionStart

Field Detail

actionListenerback to summary
pack-priv transient ActionListener actionListener
baseback to summary
private static final String base
columnsback 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.

See Also
setColumns(int), getColumns()
echoCharback 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 = 0.

See Also
getEchoChar(), setEchoChar(char), echoCharIsSet()
nameCounterback to summary
private static int nameCounter
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.awt.TextComponent.serialVersionUID.

Use serialVersionUID from JDK 1.1 for interoperability.

Annotations
@Serial
textFieldSerializedDataVersionback to summary
private int textFieldSerializedDataVersion

The textField Serialized Data Version.

Constructor Detail

TextFieldback to summary
public TextField() throws HeadlessException

Constructs a new text field.

Exceptions
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true.
See Also
java.awt.GraphicsEnvironment#isHeadless
TextFieldback to summary
public TextField(String text) throws HeadlessException

Constructs a new text field initialized with the specified text.

Parameters
text:String

the text to be displayed. If 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.

Exceptions
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true.
See Also
java.awt.GraphicsEnvironment#isHeadless
TextFieldback 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.

Parameters
columns:int

the number of columns. If columns is less than 0, columns is set to 0.

Exceptions
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true.
See Also
java.awt.GraphicsEnvironment#isHeadless
TextFieldback 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.

Parameters
text:String

the text to be displayed. If 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.

columns:int

the number of columns. If columns is less than 0, columns is set to 0.

Exceptions
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true.
See Also
java.awt.GraphicsEnvironment#isHeadless

Method Detail

addActionListenerback 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.

Parameters
l:ActionListener

the action listener.

Since
1.1
See Also
removeActionListener, getActionListeners, java.awt.event.ActionListener
addNotifyback to summary
public void addNotify()

Overrides java.awt.TextComponent.addNotify.

Creates the TextField's peer. The peer allows us to modify the appearance of the TextField without changing its functionality.

constructComponentNameback to summary
pack-priv String constructComponentName()

Overrides java.awt.Component.constructComponentName.

Construct a name for this component. Called by getName() when the name is null.

echoCharIsSetback 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.

Returns:boolean

true if this text field has a character set for echoing; false otherwise.

See Also
java.awt.TextField#setEchoChar, java.awt.TextField#getEchoChar
eventEnabledback to summary
pack-priv boolean eventEnabled(AWTEvent e)

Overrides java.awt.TextComponent.eventEnabled.

getAccessibleContextback to summary
public AccessibleContext getAccessibleContext()

Overrides java.awt.TextComponent.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

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.

Returns:AccessibleContext

an AccessibleAWTTextField that serves as the AccessibleContext of this TextField

Since
1.3
getActionListenersback to summary
public synchronized ActionListener[] getActionListeners()

Returns an array of all the action listeners registered on this textfield.

Returns:ActionListener[]

all of this textfield's ActionListeners or an empty array if no action listeners are currently registered

Since
1.4
See Also
addActionListener, removeActionListener, java.awt.event.ActionListener
getColumnsback 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.

Returns:int

the number of columns.

Since
1.1
See Also
java.awt.TextField#setColumns
getEchoCharback 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 echoChar = 0, user input is echoed to the screen unchanged.

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.

Returns:char

the echo character for this text field.

See Also
java.awt.TextField#echoCharIsSet, java.awt.TextField#setEchoChar
getListenersback to summary
public <T extends EventListener> T[] getListeners(Class<T> listenerType)

Overrides java.awt.TextComponent.getListeners.

Returns an array of all the objects currently registered as FooListeners upon this TextField. FooListeners are registered using the addFooListener method.

You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a TextField t for its action listeners with the following code:

ActionListener[] als = (ActionListener[])(t.getListeners(ActionListener.class));
If no such listeners exist, this method returns an empty array.
Parameters
listenerType:Class<T>

the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener

Returns:T[]

an array of all objects registered as FooListeners on this textfield, or an empty array if no such listeners have been added

Exceptions
ClassCastException:
if listenerType doesn't specify a class or interface that implements java.util.EventListener
Since
1.3
See Also
getActionListeners
getMinimumSizeback to summary
public Dimension getMinimumSize(int columns)

Gets the minimum dimensions for a text field with the specified number of columns.

Parameters
columns:int

the number of columns in this text field.

Returns:Dimension

the minimum size for this text field

Since
1.1
getMinimumSizeback to summary
public Dimension getMinimumSize()

Overrides java.awt.Component.getMinimumSize.

Gets the minimum dimensions for this text field.

Returns:Dimension

the minimum dimensions for displaying this text field.

Since
1.1
getPreferredSizeback to summary
public Dimension getPreferredSize(int columns)

Gets the preferred size of this text field with the specified number of columns.

Parameters
columns:int

the number of columns in this text field.

Returns:Dimension

the preferred dimensions for displaying this text field.

Since
1.1
getPreferredSizeback to summary
public Dimension getPreferredSize()

Overrides java.awt.Component.getPreferredSize.

Gets the preferred size of this text field.

Returns:Dimension

the preferred dimensions for displaying this text field.

Since
1.1
minimumSizeback to summary
public Dimension minimumSize(int 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.

Parameters
columns:int

the number of columns

Returns:Dimension

the minimum size for this text field

Annotations
@Deprecated
minimumSizeback to summary
public Dimension minimumSize()

Overrides java.awt.Component.minimumSize.

Doc from java.awt.Component.minimumSize.

Deprecated

As of JDK version 1.1, replaced by getMinimumSize().

Returns the minimum size of this component.

Annotations
@Deprecated
paramStringback to summary
protected String paramString()

Overrides java.awt.TextComponent.paramString.

Returns a string representing the state of this TextField. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

Returns:String

the parameter string of this text field

preferredSizeback to summary
public Dimension preferredSize(int 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.

Parameters
columns:int

the number of columns

Returns:Dimension

the preferred size for the text field

Annotations
@Deprecated
preferredSizeback to summary
public Dimension preferredSize()

Overrides java.awt.Component.preferredSize.

Doc from java.awt.Component.preferredSize.

Deprecated

As of JDK version 1.1, replaced by getPreferredSize().

Returns the component's preferred size.

Annotations
@Deprecated
processActionEventback to summary
protected void processActionEvent(ActionEvent e)

Processes action events occurring on this text field by dispatching them to any registered ActionListener objects.

This method is not called unless action events are enabled for this component. Action events are enabled when one of the following occurs:

  • An ActionListener object is registered via addActionListener.
  • Action events are enabled via enableEvents.

Note that if the event parameter is null the behavior is unspecified and may result in an exception.

Parameters
e:ActionEvent

the action event

Since
1.1
See Also
java.awt.event.ActionListener, java.awt.TextField#addActionListener, java.awt.Component#enableEvents
processEventback to summary
protected void processEvent(AWTEvent e)

Overrides java.awt.TextComponent.processEvent.

Processes events on this text field. If the event is an instance of ActionEvent, it invokes the processActionEvent method. Otherwise, it invokes processEvent on the superclass.

Note that if the event parameter is null the behavior is unspecified and may result in an exception.

Parameters
e:AWTEvent

the event

Since
1.1
See Also
java.awt.event.ActionEvent, java.awt.TextField#processActionEvent
readObjectback to summary
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException, HeadlessException

Hides java.awt.TextComponent.readObject.

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.

Parameters
s:ObjectInputStream

the ObjectInputStream to read

Annotations
@Serial
Exceptions
ClassNotFoundException:
if the class of a serialized object could not be found
IOException:
if an I/O error occurs
HeadlessException:
if GraphicsEnvironment.isHeadless() returns true
See Also
removeActionListener(ActionListener), addActionListener(ActionListener), java.awt.GraphicsEnvironment#isHeadless
removeActionListenerback 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.

Parameters
l:ActionListener

the action listener.

Since
1.1
See Also
addActionListener, getActionListeners, java.awt.event.ActionListener
replaceEOLback to summary
private static String replaceEOL(String text)

Replaces EOL characters from the text variable with a space character.

Parameters
text:String

the new text.

Returns:String

Returns text after replacing EOL characters.

setColumnsback 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.

Parameters
columns:int

the number of columns.

Exceptions
IllegalArgumentException:
if the value supplied for columns is less than 0.
Since
1.1
See Also
java.awt.TextField#getColumns
setEchoCharback 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 echoChar = 0 allows user input to be echoed to the screen again.

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.

Parameters
c:char

the echo character for this text field.

Since
1.1
See Also
java.awt.TextField#echoCharIsSet, java.awt.TextField#getEchoChar
setEchoCharacterback to summary
public synchronized void setEchoCharacter(char c)

Deprecated

As of JDK version 1.1, replaced by setEchoChar(char).

Sets the character to be echoed when protected input is displayed.

Parameters
c:char

the echo character for this text field

Annotations
@Deprecated
setTextback to summary
public void setText(String t)

Overrides java.awt.TextComponent.setText.

Sets the text that is presented by this text component to be the specified text.

Parameters
t:String

the new text. If 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.

See Also
java.awt.TextComponent#getText
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Hides java.awt.TextComponent.writeObject.

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.

Parameters
s:ObjectOutputStream

the ObjectOutputStream to write

Annotations
@Serial
Exceptions
IOException:
if an I/O error occurs
Serial data
Null terminated sequence of zero or more pairs. A pair consists of a String and Object. The String indicates the type of object and is one of the following : ActionListenerK indicating and ActionListener object.
See Also
AWTEventMulticaster#save(ObjectOutputStream, String, EventListener), java.awt.Component#actionListenerK
java.awt back to summary

protected Class TextField.AccessibleAWTTextField

extends AccessibleAWTTextComponent
Class Inheritance

This class implements accessibility support for the TextField class. It provides an implementation of the Java Accessibility API appropriate to text field user-interface elements.
Since
1.3

Field Summary

Modifier and TypeField and Description
private static final long
serialVersionUID

Hides java.awt.TextComponent.AccessibleAWTTextComponent.serialVersionUID.

Use serialVersionUID from JDK 1.3 for interoperability.

Constructor Summary

AccessConstructor and Description
protected
AccessibleAWTTextField()

Constructs an AccessibleAWTTextField.

Method Summary

Modifier and TypeMethod and Description
public AccessibleStateSet

Returns:

an instance of AccessibleStateSet describing the states of the object
getAccessibleStateSet
()

Overrides java.awt.TextComponent.AccessibleAWTTextComponent.getAccessibleStateSet.

Gets the state set of this object.

Inherited from java.awt.TextComponent.AccessibleAWTTextComponent:
getAccessibleRolegetAccessibleTextgetAfterIndexgetAtIndexgetBeforeIndexgetCaretPositiongetCharacterAttributegetCharacterBoundsgetCharCountgetIndexAtPointgetSelectedTextgetSelectionEndgetSelectionStarttextValueChanged

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.awt.TextComponent.AccessibleAWTTextComponent.serialVersionUID.

Use serialVersionUID from JDK 1.3 for interoperability.

Annotations
@Serial

Constructor Detail

AccessibleAWTTextFieldback to summary
protected AccessibleAWTTextField()

Constructs an AccessibleAWTTextField.

Method Detail

getAccessibleStateSetback to summary
public AccessibleStateSet getAccessibleStateSet()

Overrides java.awt.TextComponent.AccessibleAWTTextComponent.getAccessibleStateSet.

Gets the state set of this object.

Returns:AccessibleStateSet

an instance of AccessibleStateSet describing the states of the object

See Also
AccessibleState