When to validate fields from a swing form?

Asked

Viewed 502 times

2

I was wondering if someone could help me with the validation of forms fields in java desktop. When should I perform validations, in the field (focusLost) output or in Keystroke(keyPressed), or other more appropriate form.

For example:

I got a field like JFormatTextField with the ##/##/##################(date), with a field that is required, I want to leave the window, so I click the exit button, it validates the field anyway, because the validation is in Focus, another question and this validation is in keypress, if I do not press enter and exit the field it will not validate, which 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;
            }
        }
    };

Stantiated in the builder

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

Action for the button to exit

private void ActionSair() {
        this.dispose();
}
  • It depends on your application, but I would not use the loss of focus to validate a field, but rather, alert the user about the completion of this.

  • Ulysses, instead of posting the solution in the answer, you can answer your own question with it. To stay in agreement with the site, you might want to do this. You can also mark your solution as the correct one.

  • Okay, I was going to do this, but a question came up if I really wanted to do this, I was in doubt, sorry. I’m still getting used to the site.

  • What are these added codes? If they are responses, as I told you, you should edit your answer below and add to it.

  • diegofm take a look at the comments of my answer that you will understand what is happening.

2 answers

1

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

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);

1


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

I hope I’ve given you an idea of how to proceed.

  • It is possible to use also the InputVerifier, that will perform the validations at runtime, without having to wait for Ubmit to validate. It’s more complicated, but saves a few lines of code with numerous listeners.

  • I did not know the Inputverifier , But thanks for the tip, I will research on the subject.

  • I did it the way Edumachdo had said, I think even better, but the boss seasick and full of freshness, likes validation in the field, is copying idea of another system used here. I used inputVerifier and it worked.

  • I found only one problem in inputverifier, it did what I wanted, but when I click the cancel button(which is a Dispose) to exit the window, it does not allow because the field is not filled.

  • Can you post your complete code?? at least the Buttons and inputVerifier. So that we can analyze the problem better.

  • Check if it helps. I put inputverifier and boot code

Show 1 more comment

Browser other questions tagged

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