I have a TextFormatter
:
mailTextField.setTextFormatter(new TextFormatter<>(change -> {
int maxLength = 100;
if (change.isAdded()) {
if(change.getControlNewText().length()>maxLength){
if(change.getText().length()==1){
change = null;
System.out.println("Reached max!");
}else{
int allowedLength = maxLength - change.getControlText().length();
change.setText(change.getText().substring(0, allowedLength));
System.out.println("Cut paste!");
}
}
if(change!=null){
System.out.println("Mail check: "+change.getControlNewText());
if(Validation.mail(change.getControlNewText())){
showCorrectIcon(mailTextField);
}else{
showErrorIcon(mailTextField);
}
}
}
return change;
}));
Since initial mail is stored in database, I don't want to show correct icon for it, but only if user tries to adjust it. Is it possible to make TextFormatter
work only in case mail field is in focus?