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:
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("This is a text");
JTextField textField = new JTextField(20);
JTextField textField = new JTextField("This is a text", 20);
JTextField textField = new JTextField(); textField.setText("This is a text"); textField.setColumns(20);
NOTE:
frame.add(textField); dialog.add(textField); panel.add(textField); applet.getContentPane().add(textField);
frame.add(textField, BorderLayout.CENTER); panel.add(textField, gridbagConstraints);
String content = textField.getText();
int offset = 5; int length = 10; try { content = textField.getText(offset, length); } catch (BadLocationException ex) { // invalid offset/length }
That will return 10 characters from position 5th in the text.
textField.setText("another text");
Set tooltip for the text field as follows:
textField.setToolTipText("Please enter some text here");
Image:
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:
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:
frame.setVisible(true); textField.requestFocusInWindow();
SwingUtilities.invokeLater(new Runnable() { @Override public void run() { textField.requestFocusInWindow(); } });
textField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { System.out.println("The entered text is: " + textField.getText()); } });
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.
We can programmatically select the text field’s content.
textField.selectAll();
textField.setSelectionStart(8); textField.setSelectionEnd(12);
textField.setSelectionColor(Color.YELLOW); textField.setSelectedTextColor(Color.RED);
textField.setCaretColor(Color.RED); textField.setCaretPosition(10);
Image:
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.CENTER);
Valid values for the method setHorizontalAlignment() are:
JTextField.LEFT JTextField.CENTER JTextField.RIGHT JTextField.LEADING JTextField.TRAILING
textField.setFont(new java.awt.Font("Arial", Font.ITALIC | Font.BOLD, 12)); textField.setForeground(Color.BLUE); textField.setBackground(Color.YELLOW);
Image:
For your reference and testing purpose, we created a simple demo program looks like this:
When you are typing something in the text field and hit Enter, the following message dialog appears:
If the field is empty, the OK button is disabled:
And when clicking the button:
You can download, run and view source code of this program in the attachment section.