The first thing I do is create a NumberFormat
that is appropriate to my needs:
this.format = NumberFormat.getIntegerInstance();
this.format.setMinimumIntegerDigits(6);
this.format.setMaximumIntegerDigits(6);
this.format.setGroupingUsed(false);
I then apply it to a JFormattedTextField:
this.fld_id = new JFormattedTextField(this.format);
I then set this field's InputVerifier
, like so:
this.fld_id.setInputVerifier(new IdVerifier());
My IdVerifier
simply looks to see that the entered text conforms to a regular expression:
private class IdVerifier extends InputVerifier {
public boolean verify(final JComponent arg0) {
final JFormattedTextField verifyJTF = (JFormattedTextField) arg0;
if (!verifyJTF.getText().matches("\\d{6}")) {
return false;
}
return true;
}
public boolean shouldYieldFocus(final JComponent arg0) {
if (!this.verify(arg0)) {
arg0.setBackground(Color.RED);
} else {
arg0.setBackground(UIManager.getColor("TextField.background"));
}
return true;
}
}
However, when I do this, I enter a value into the JFormattedTextField
, let's say "1". This gets reformatted to "000001" by the NumberFormatter
. However, in this example, the background of the text field is still set to red.
What is the correct way to ensure that the formatted contents of the JFormattedTextField
are what gets consumed by the InputVerifier
? I expected the default behavior to be that the focus is lost, the formatter is applied, then the input is verified. However, that's clearly not the case.