By Tony Bevis on Wednesday, 18 October 2023
Category: Java

Java programming course: 14.3 JTextField

JTextField

The JTextField class allows the entry by the user of a piece of text in a box consisting of a single line:


You can make the field show some initial default text by using the two-argument constructor:

Your frame should look like this:

Like the JLabel class, you can use methods setText() and getText() to change and obtain the text value in the entry box.

The JTextField class is frequently used in combination with the JLabel class to give the user an indication of what type of information is expected to be entered. You could add each of these to a left-aligned flow layout, for example:

The frame should look as follows:

If you want the label above the text field instead of to its left, you could place them in a single-column grid:

Which will look like this:

Next lesson: 14.4 JTextArea

Related Posts

Leave Comments