Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2004

Tip: Looking for answers? Try searching our database.

Validation techniques for JFormattedTextFields

Thread view: 
Steve Webb - 16 Aug 2004 11:25 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
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.