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>‣ 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>‣ 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> ‣ 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>‣ 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.
– user28595
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.
– user28595
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.
– Ulisses Gimenes
What are these added codes? If they are responses, as I told you, you should edit your answer below and add to it.
– user28595
diegofm take a look at the comments of my answer that you will understand what is happening.
– Edumachdo