Cyberborean Chronicles

[Tips & Tricks:] Required text fields in Swing


This article starts a “Tips & Tricks” serie of “Chronicles” which is a result of my comeback to heavy coding. The posts in this serie are the bits of coding experience, a small inventions and solutions which every programmer does everyday. Read the latest Tips&Tricks in Technology::Coding category.

Swing dialogs by default have no idea about “required” text fields, that is the fields which should be filled to perform a task. In this article I suggest a simple way how to automatically highlight the fields which have to be non-empty.

Our required fields will be highlighted when empty and switched to default look when a user enters a data into them. To track the changes of a field’s content we will use DocumentEvent/DocumentListener mechanism. DocumentEvent‘s are generated by underlying Document model when it is changed by some way. Listening to these events is more reliable way than tracking the user actions (such as key pressings) directly, because it is guaranteed that any content change (including possible changes made by program itself, pasting the clipboard content from a context menu etc) will be monitored.

To listen these events, we should create an implementation of DocumentListener interface:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.event.*;
import javax.swing.text.JTextComponent;
...
 
public class HighlightListener implements DocumentListener {
 
    JTextComponent comp = null;
    Border defaultBorder = null;
    Border highlightBorder =
            BorderFactory.createLineBorder(java.awt.Color.ORANGE);
 
    public HighlightListener(JTextComponent jtc) {
        comp = jtc;
        defaultBorder = comp.getBorder();
        // Adding this listener to a specified component:
        comp.getDocument().addDocumentListener(this);
        // Highlight if empty:
        this.maybeHighlight();
    }
 
    public void insertUpdate(DocumentEvent e) {
        maybeHighlight();
    }
 
    public void removeUpdate(DocumentEvent e) {
        maybeHighlight();
    }
 
    public void changedUpdate(DocumentEvent e) {
        maybeHighlight();
    }
 
    private void maybeHighlight() {
        if (comp.getText().trim().length() > 0)
            // if a field is non-empty, switch it to default look
            comp.setBorder(defaultBorder);
        else
            // if a field is empty, highlight it
            comp.setBorder(highlightBorder);
        // ... more actions
    }
}

To add a highlighting functionality to a text component we should simply instantiate our HighlightListener with this component:

...
JTextField myField = new JTextField();
new HighlightListener(myField);
...

Note that HighlightListener constructor is parametrized with JTextComponent class, so it can work with any children (JEditorPane, JTextArea), not only with JTextField.

Our HighlightListener monitors the updates of a component document model and checks if the text is empty or not. If it is empty, it will be highlighted by adding a thin orange border:

Otherwise, the border will be reverted to its default look.

The code of HighlightListener can be customized for:

  • Another highlighting method
  • Additional highlighting conditions – e.g. we can check the content against a regular expression for required input format (valid email adresses for instance)
  • Additional actions – e.g. to enable/disable the “OK” dialog button if required data is not entered.

Leave a Reply