How to validate a text field with InputVerifier?

3

Normally when I need to validate a text field without using action events, I use focus events, such as those available through the FocusListener .

In this example, I check if the field was filled out when I lost focus, and I returned the focus to it with a red border if it was not already filled:

textField.addFocusListener(new FocusAdapter(){

    Border originalBorder;

    @Override
    public void focusLost(FocusEvent e){

        JTextComponent comp = (JTextComponent) e.getSource();

        if(comp.getText().trim().isEmpty()){
            originalBorder = originalBorder == null ? comp.getBorder() : originalBorder;
            comp.setBorder(BorderFactory.createLineBorder(Color.red, 2));
            comp.requestFocus();
    } else {
            if(originalBorder != null) {
                input.setBorder(originalBorder);
                originalBorder = null;
            }
        }
});

However, I discovered that the swing API has the class InputVerifier , which allows me to do the same thing without having to spend focus listeners, which may be useful for other features.

How does this class InputVerifier work and how do I use it to ensure that a field is populated before losing focus, as in the example?

    
asked by anonymous 06.10.2017 / 16:07

1 answer

3

The purpose of the InputVerifier class is to allow navigation control between text components through the focus, where it is possible to restrict the field change if the values filled in do not meet certain criteria defined in the program.

The class has two abstract methods:

  • public abstract boolean verify(JComponent input){} - This method is used to check whether the information entered in the field is valid or not, by returning a boolean value. It should not return dialog boxes or equivalents to display to the user that the field entry is invalid.

  • public boolean shouldYieldFocus(JComponent input){} - This method calls verify to validate the field. It is invoked when the user attempts to change the focus of the component being validated to another in the same window. Its return is that it defines whether or not the focus can be changed, so it can be used to return messages on the screen, such as dialog boxes.

Based on the proposed example, just set a new InputVerifier for the field, passing the focusLost validation to the verify() method, and according to its return, apply the changes in the field using the shouldYieldFocus() to alert you about filling in:

textField.setInputVerifier(new InputVerifier() {

    Border originalBorder;

    @Override
    public boolean verify(JComponent input) {

        JTextField comp = (JTextField) input;
        return !comp.getText().trim().isEmpty();
    }

    @Override
    public boolean shouldYieldFocus(JComponent input) {

        boolean isValid = verify(input);

        if (!isValid) {
            originalBorder = originalBorder == null ? input.getBorder() : originalBorder;
            input.setBorder(BorderFactory.createLineBorder(Color.red, 2));
        } else {
            if(originalBorder != null) {
                input.setBorder(originalBorder);
                originalBorder = null;
            }
        }
        return isValid;
    }
});

Applying to the field would give result similar to the following:

  

Obs. : This validation is only valid for navigation through focus, either by clicking other components with the mouse or by using the TAB key, but it does not prevent a form is submitted through an ActionListener of a button or otherwise.

    
06.10.2017 / 16:07