S
Steve Webb
Hi,
I've got a JFormattedTextfield which I've set a formatter, enter key
listener and InputVerifier on and it all works fine when the user
enters data and pressed enter or tabs out of the field. The checks are
made fine. However if the program itself calls setValue() or setText()
on the field the InputVerifier gets called and then fails. The problem
seems to be that even though the text is now shown in the field the
call to getText in the verifier returns an empty string !!! The
verifier is shown below, any ideas ?
class ssccVerifier extends InputVerifier {
public ssccVerifier() {
}
public boolean verify(JComponent input) {
if(!(input instanceof JFormattedTextField))
return true;
JFormattedTextField jftf = (JFormattedTextField)input;
NumberFormatter formatter =
(NumberFormatter)jftf.getFormatter();
if(formatter == null)
return true;
try {
String content = jftf.getText();
Long v = (Long)formatter.stringToValue(content);
jftf.setValue(v);
clearError();
return true;
} catch (ParseException pe) {
jftf.selectAll();
displayError("Valid SSCC is between " +
formatter.getMinimum() + " and " + formatter.getMaximum());
}
return false;
}
}
I've got a JFormattedTextfield which I've set a formatter, enter key
listener and InputVerifier on and it all works fine when the user
enters data and pressed enter or tabs out of the field. The checks are
made fine. However if the program itself calls setValue() or setText()
on the field the InputVerifier gets called and then fails. The problem
seems to be that even though the text is now shown in the field the
call to getText in the verifier returns an empty string !!! The
verifier is shown below, any ideas ?
class ssccVerifier extends InputVerifier {
public ssccVerifier() {
}
public boolean verify(JComponent input) {
if(!(input instanceof JFormattedTextField))
return true;
JFormattedTextField jftf = (JFormattedTextField)input;
NumberFormatter formatter =
(NumberFormatter)jftf.getFormatter();
if(formatter == null)
return true;
try {
String content = jftf.getText();
Long v = (Long)formatter.stringToValue(content);
jftf.setValue(v);
clearError();
return true;
} catch (ParseException pe) {
jftf.selectAll();
displayError("Valid SSCC is between " +
formatter.getMinimum() + " and " + formatter.getMaximum());
}
return false;
}
}