How do I read text from a JTextField in Java Swing?

Call getText() on a JTextField to read what the user has typed. In this example, the user enters a name and clicks Say hello or presses Enter to display a greeting.

Source code

Save the following code as TextFieldExample.java:

import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class TextFieldExample {
    void main() {
        SwingUtilities.invokeLater(this::showWindow);
    }

    private void showWindow() {
        JTextField nameField = new JTextField(14);
        JLabel nameLabel = new JLabel("Name:");
        nameLabel.setLabelFor(nameField);

        JButton greetButton = new JButton("Say hello");
        JLabel greeting = new JLabel(
                "Enter your name.", SwingConstants.CENTER);

        greetButton.addActionListener(event ->
                updateGreeting(nameField, greeting));
        nameField.addActionListener(event ->
                updateGreeting(nameField, greeting));

        JPanel form = new JPanel();
        form.add(nameLabel);
        form.add(nameField);
        form.add(greetButton);

        JPanel content = new JPanel(new BorderLayout(0, 12));
        content.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
        content.add(form, BorderLayout.PAGE_START);
        content.add(greeting, BorderLayout.CENTER);

        JFrame frame = new JFrame("Text Field Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(content);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void updateGreeting(JTextField nameField, JLabel greeting) {
        String name = nameField.getText().strip();

        if (name.isEmpty()) {
            greeting.setText("Please enter your name.");
        } else {
            greeting.setText("Hello, " + name + "!");
        }
    }
}

What the window looks like

Illustration of the Swing window showing Rosa in the text field and Hello, Rosa! below it

Explanation

new JTextField(14) creates a single-line text field. The number 14 helps Swing calculate its preferred width. The user can still type more than 14 characters.

The updateGreeting() method reads the field when the user submits the name:

String name = nameField.getText().strip();

getText() returns the current text. strip() removes whitespace from both ends of the returned string without changing what appears in the field. If the result is empty, the label asks for a name. Otherwise, it displays a greeting.

Both the button and the text field have an action listener that calls updateGreeting(). The button listener runs when the button is activated. The text field listener runs when the user presses Enter while the field has focus. Both listeners run on Swing’s event dispatch thread and can update the label directly.

Run the example

javac TextFieldExample.java
java TextFieldExample

Type Rosa and click Say hello. The label displays Hello, Rosa!. Clear the field and press Enter to see Please enter your name.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.