JTextFieldis a fundamental Swing’s component that allows users editing a single line of text. This article lists common practices when using JTextField in Swing development.

Table of content:

    1. Creating a JTextField object
    2. Adding the text field to a container
    3. Getting or setting content of the text field
    4. Setting tooltip text
    5. Setting input focus
    6. Adding event listeners
    7. Working with text selection
    8. Customizing JTextField’s appearance
    9. JTextField demo program

 

1. Creating a JTextField object

When creating a text field component, it’s common to specify some initial text and/or a number of columns from which the field’s width is calculated.

JTextField textField = new JTextField();
textField.setText("This is a text");
textField.setColumns(20);
 

NOTE:

2. Adding the  text field to a container

frame.add(textField, BorderLayout.CENTER);
panel.add(textField, gridbagConstraints);
  

3. Getting or setting content of the text field

textField.setText("another text");
 

4. Setting tooltip text for JTextField

Set tooltip for the text field as follows:

textField.setToolTipText("Please enter some text here");
 

Image:

set tooltip for text field

We can also set HTML for the tooltip text:

textField.setToolTipText("<html><b><font color=red>"
					+ "Please enter some text here" + "</font></b></html>");
 

Image:

html tooltip text field

5. Setting input focus for JTextField

Normally, the text field gets focused when the user is clicking on it or pressing the TAB key. To set input focus programmatically, use the following code:

SwingUtilities.invokeLater(new Runnable() {
	@Override
	public void run() {
		textField.requestFocusInWindow();
	}
});

 

6. Adding event listeners for JTextField

textField.addKeyListener(new KeyListener() {

	@Override
	public void keyTyped(KeyEvent event) {
		System.out.println("key typed");
	}

	@Override
	public void keyReleased(KeyEvent event) {
		System.out.println("key released");
	}

	@Override
	public void keyPressed(KeyEvent event) {
		System.out.println("key pressed");
	}
});

 

The order of key events is key pressed, key typed and key released. We can use this technique to validate field’s content on-the-fly. In the following example, we check the field’s content whenever the user is typing. If the content is empty, disable the action button; otherwise enable the button:

textField.addKeyListener(new KeyAdapter() {
	public void keyReleased(KeyEvent event) {

		String content = textField.getText();
		if (!content.equals("")) {
			button.setEnabled(true);
		} else {
			button.setEnabled(false);
		}
	}
});

NOTE: In this case, we use the KeyAdapterclass which implements the KeyListener interface, so we have to override only the method we want.

 
 

7. Working with text selection in JTextField

We can programmatically select the text field’s content.

textField.setCaretColor(Color.RED);
textField.setCaretPosition(10);

Image: customize caret in text field

 

8. Customizing JTextField’s appearance

textField.setFont(new java.awt.Font("Arial", Font.ITALIC | Font.BOLD, 12));
textField.setForeground(Color.BLUE);
textField.setBackground(Color.YELLOW);

Image: colorful text field

 

9. JTextField demo program

For your reference and testing purpose, we created a simple demo program looks like this:

JTextField demo program

When you are typing something in the text field and hit Enter, the following message dialog appears:

message dialog 1

If the field is empty, the OK button is disabled:

button disabled

And when clicking the button:

message dialog 2

You can download, run and view source code of this program in the attachment section.

 

Other Java Swing Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (SwingJTextFieldDemo.zip)SwingJTextFieldDemo.zip[Source code and runnable jar file]11 kB