InputVerifier and JFormattedTextField Problem :(

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

}
 
W

Will

try this (i haven't tested this)

String content = jftf.getText();
if (content != null && !content.equals("")) {
Long v = (Long)formatter.stringToValue(content);
jftf.setValue(v);
}
clearError();
return true;

Regards,
Will
 
W

Webby

Thanks for that Will. I'd already added something similar as a work around so great minds think
alike. The problem I have though is that when the setText() or setValue() is used the value passed
is from another system which could pass invalid data so the idea was that the normal validation on
the field would either flag a problem up or not.

Any idea's why setValue() and/or setText() on a JFormattedTextField with an InputValidator causes
the getText() not to function as expected in the veryify routine ? Strikes me its some kind of Java
bug on the face of it.
 
W

Will

Any idea's why setValue() and/or setText() on a JFormattedTextField with an InputValidator causes
the getText() not to function as expected in the veryify routine ? Strikes me its some kind of Java
bug on the face of it.


I think after setValue/setText you should do a commitEdit()


Will
author of JForm
jform.coderight.nl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top