Im looking for some info on standard techniques for validation of text
fields in JFormattedTextFields. They seem to have masks, input
validation methods, key listeners and so on which frankly seem a pain
in the backside to use. As an example I have one field which I want to
restrict the user to entering a value which is 0-N in value (N being
variable). I want to stop the use from leaving the field unless they
enter a valid value. I've got all this to work but its not nice and
seems hard to do. I ended up doing the following :-
Create the JFormattedTextField
Create a Numberformatter
SetFomratFactory on the text field using the formatter
Setup and InputVerifier on the textfield
Setup and input and actionmap for the enter key being pressed on the
text field
In the Input Verifier :
Get the formatter from the textfield
call stringbyvalue on the formatter to get the int value
if an int is returned without throwing an exception setvalue on the
setfield
if no expceptions are thrown return true otherwise return false.
In the actionmap:
basically the same as the input verifier but don't return anything.
If data is invalid I select everything in the textfield and don't
allow focus to be lost.
Now this all strikes me as a long winded way to do something that
should be fairly simple. I really just want a way of saying don't
allow focus to be lost in anyway unless the data is valid and here is
the check method. Is there a way to do this ?
Steve
Karsten Lentzsch - 16 Aug 2004 12:15 GMT
> Im looking for some info on standard techniques for validation [...]
The JGoodies Validation Demo shows a couple of
validation styles, validation result renderers,
configurations for the JFormattedTextField, see
http://www.jgoodies.com/freeware/validationdemo/
I tried to collect typical validation issues:
when to validate, who validates, how to provide
input hints, how to display invalid input,
how to filter invalid input, how to drive the
JFormattedTextField, how to configure formatters.
Just see yourself.
Hope this helps. Regards,
Karsten Lentzsch
VisionSet - 16 Aug 2004 14:34 GMT
> Im looking for some info on standard techniques for validation of text
> fields in JFormattedTextFields. They seem to have masks, input
> validation methods, key listeners and so on which frankly seem a pain
> in the backside to use. As an example I have one field which I want to
> restrict the user to entering a value which is 0-N in value (N being
...
If you can live with '000000' as the default value visible in the field
then use a MaskFormatter("#######") with the place holder set to '0'
That is the simplest way to ensure an integer is valid. is that what you
meant by 0-N ?
--
Mike W
david - 20 Aug 2004 07:46 GMT
> If you can live with '000000' as the default value visible in the field
> then use a MaskFormatter("#######") with the place holder set to '0'
> That is the simplest way to ensure an integer is valid. is that what you
> meant by 0-N ?
I'd also recommend a MaskFromatter. If you don't want the '0000'
simply create a custom fomratter where you set teh valid characters to
"0123456789".
d.