Java Reference
In-Depth Information
The TextField class contains four constructors that take in a variation of the
following two parameters:
String text. Represents the initial text that appears in the TextField.
int columns. The initial size of the TextField, based on the platform-
dependent average width of the current character font.
In addition to generating a TextEvent when the text changes, a TextField
also generates an ActionEvent when the user hits the Enter key while typing in
the text field. Therefore, the TextField class contains the addActionListener()
method for registering an ActionListener. Handling the action event is com-
monly done because users often expect some behavior to happen when they
press Enter while in a text field. The TextComponentDemo program that I will
discuss shortly demonstrates doing this.
The TextArea class has five constructors that take in a variation of the fol-
lowing parameters:
String text. Represents the initial text displayed in the text area.
int rows. The number of rows of text to display.
int columns. The number of columns to display, based on the platform-
dependent average width of the current character font.
int scrollbars. Denotes the scrollbar property of the text area. The possible
values are static fields in the TextArea class: SCROLLBARS_NONE,
SCROLLBARS_VERTICAL_ONLY, SCROLLBARS_HORIZONTAL_ONLY,
and SCROLLBARS_BOTH.
Both the TextField and TextArea constructors allow you to assign an initial
number of columns and rows. The actual size of these values depends on
the character font being displayed in the text component.
Keep in mind that the values you pass in to these constructors for rows
and columns become the preferred size of the text component. As with all
components in both Swing and AWT, most layout managers override a
component's preferred size. The TextComponentDemo program in this
chapter creates a TextField and TextArea whose rows and columns are
zero because both are laid out with a BorderLayout manager that
overrides any preferred size anyway.
A TextArea does not generate an ActionEvent as a TextField does. In most
situations, you will not listen to the events generated from a text area. Instead,
another component such as a button or menu item often signals when a user is
done entering text in a text area.
Search WWH ::




Custom Search