When to validate fields of a form in swing?

2

I would like to know if anyone could help me on validating form fields in java desktop. When should I perform the validations, at the field's focusLost or keyPressed, or another more appropriate way.

For example:

I have a field of type JFormatTextField with the mask of ## / ## / #### (date), being the field that is mandatory, I want to exit the window, then I click the exit button, it validates the field anyway, because the validation is in the focus, another question and this validation being in the keyPress, if I do not press enter and leave the field it will not validate, what is the correct logic for this type of validation

private void txtDataCotFocusLost(java.awt.event.FocusEvent evt) {                                     
        if (txtDataCot.getText().equals("  /  /    ")) {
            //Limpa o lixo no campo
            txtDataCot.setValue(null);
            //Mensagem de validação
            MensagemPersJOptionPane.msgAttention(rootPane, "<html>Conflitos encontrados, Verifique:<br><br>&#8227; Informe a data do orçamento!</html>", MensagemPersJOptionPane.getMsgTitleValidacao());
            txtDataCot.requestFocus();
        }
    } 

InputVerifiers:

InputVerifier verifierQuant = new InputVerifier() {
        @Override
        public boolean verify(JComponent input) {
            final JTextComponent source = (JTextComponent) input;
            String text = source.getText();
            double number = Double.parseDouble(text.replace(".", "").replace(",", "."));
            if (number == 0 && !sair.equals("Sair")) {
                MensagemPersJOptionPane.msgAttention(rootPane, "<html>Conflito(s) encontrado(s), verifique! <br><br>&#8227; Não foi informada uma quantidade </html>",
                        MensagemPersJOptionPane.getMsgTitleValidacao());
                return false;
            } else {
                return true;
            }
        }
    };

    InputVerifier verifierProduct = new InputVerifier() {
        @Override
        public boolean verify(JComponent input) {
            final JTextComponent source = (JTextComponent) input;
            String text = source.getText();
            if (text.length() == 0 && !sair.equals("Sair")) {
                MensagemPersJOptionPane.msgAttention(rootPane, "<html>Conflito(s) encontrado(s), verifique! <br><br> &#8227; O produto não foi informado</html>",
                        MensagemPersJOptionPane.getMsgTitleValidacao());
                return false;
            } else {
                return true;
            }
        }
    };

InputVerifier verifier = new InputVerifier() {
        @Override
        public boolean verify(JComponent input) {
            final JTextComponent source = (JTextComponent) input;
            String text = source.getText();
            if ((text.equals("  /  /    "))) {
                MensagemPersJOptionPane.msgAttention(rootPane,
                        "<html>Conflitos encontrados, Verifique:<br><br>&#8227; Informe a data do orçamento!</html>",
                        MensagemPersJOptionPane.getMsgTitleValidacao());
                return false;
            } else {
                return true;
            }
        }
    };

Dropped in the constructor

txtQuant.setInputVerifier(verifierQuant);
txtProduto.setInputVerifier(verifierProduct);
txtDataCot.setInputVerifier(verifier);

Action for the exit button

private void ActionSair() {
        this.dispose();
}
    
asked by anonymous 25.07.2016 / 13:50

2 answers

1

As a form, you must have a Button to save or proceed, depending on your form. When I need to assemble forms in this way, I always put a method to check if there is any empty field by pressing the SAVE Button, if it exists, I open a user alert window so that it can fill in the field before proceeding. p>

I hope I have given you an idea of how to proceed.

    
25.07.2016 / 14:03
1

With the answers I received and with the help of the staff, I resolved this:

InputVerifier verifier = new InputVerifier() {
        @Override
        public boolean verify(JComponent input) {
            final JTextComponent source = (JTextComponent) input;
            String text = source.getText();
            if ((text.equals("  /  /    "))) {
                MensagemPersJOptionPane.msgAttention(rootPane,
                        "<html>Conflitos encontrados, Verifique:<br><br>&#8227; Informe a data do orçamento!</html>",
                        MensagemPersJOptionPane.getMsgTitleValidacao());
                return false;
            } else {
                return true;
            }
        }
    };

txtDataCot.setInputVerifier(verifier);
    
26.07.2016 / 12:54