JTextField Class
Package: javax.swing
Using JTextField creates a text field, which is a box in which the user can type text. Figure 5-13 shows an example of a typical text field.

Figure 5-13
Constructors
|
Constructor |
Description |
|
|
Creates a new text field |
|
|
Creates a new text field with the specified width |
|
|
Creates a new text field with the specified width and initial text value |
Methods
|
Method |
Description |
|
|
Gets the text value entered in the field. |
|
|
Asks for the focus to be moved to this text field. |
|
|
Sets the size of the text field. (It’s better to do this in the constructor.) |
|
|
If |
|
|
Sets the field’s text value. |
|
|
Sets the tooltip text that’s displayed if the user rests the mouse over the text field for a few moments. |
When you create a text field by calling a constructor of the JTextField class, you can specify the width of the text field and an initial text value, as in these examples:
JTextField text1 = new JTextField(15);
JTextField text2 = new JTextField(“Initial Value”, 20);
The width is specified in columns, which is a vague and imprecise measurement that’s ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access