For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2004 > InputVerifier and JFormattedTextField Problem :(









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author InputVerifier and JFormattedTextField Problem :(
Steve Webb

2004-04-27, 10:54 am

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

}
Will

2004-04-27, 4:59 pm

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
Webby

2004-04-27, 7:11 pm

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.


"Will" <will@coderight.nl> wrote in message news:f5a0996b.0404271131.753baf70@posting.google.com...
> 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



Will

2004-04-28, 3:52 pm

> 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


"Webby" <steve@swebb99.fs2.com> wrote in message news:<c6mmkt$tma$1@news.freedom2surf.net>...[color=darkred]
> 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.
>
>
> "Will" <will@coderight.nl> wrote in message news:f5a0996b.0404271131.753baf70@posting.google.com...
Webby

2004-04-30, 1:21 pm

Ok Will I'll try that tomorrow and see what happens.

Thanks

Steve

"Will" <will@coderight.nl> wrote in message news:f5a0996b.0404281055.8e9ca65@posting.google.com...[color=darkred]
causes[color=darkred]
Java[color=darkred]
>
>
> I think after setValue/setText you should do a commitEdit()
>
>
> Will
> author of JForm
> jform.coderight.nl
>
>
> "Webby" <steve@swebb99.fs2.com> wrote in message news:<c6mmkt$tma$1@news.freedom2surf.net>...
passed[color=darkred]
on[color=darkred]
causes[color=darkred]
Java[color=darkred]
news:f5a0996b.0404271131.753baf70@posting.google.com...[color=darkred]


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com