How to validate a text field with Inputverifier?

Asked

Viewed 395 times

2

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

In this example, I check if the field was filled when losing focus, and return the focus to it, with a red border, if it has not been 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 found that the swing API has the class Inputverifier, that allows me to do the same thing without having to spend focus listeners, which can be useful for other resources.

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

1 answer

3


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

The class has two abstract methods:

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

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

Starting from the proposed example, just configure a new InputVerifier to the field, passing the validation of the focusLost for the method verify(), and as they return, apply the changes in the field through the method shouldYieldFocus() to alert about its completion:

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 similar result to the following:

inserir a descrição da imagem aqui

Obs.: this validation is only valid for navigating through focus, either by clicking on other components with the mouse or through the key TAB, but it does not prevent a form from being submitted through a Actionlistener button or otherwise.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.