*
The moose likes Swing / AWT / SWT and the fly likes Validation for Textfield Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Validation for Textfield" Watch "Validation for Textfield" New topic
Author

Validation for Textfield

RaviSingh Kumar
Ranch Hand

Joined: Sep 04, 2007
Posts: 52
Want to validate the textfield as soon as some invalid text is entered.
for e.g. If is EmpID field, error message is shown as soon as some non-integer
value is entered.

I had tried it to validate once the whole form is submitted,but unable to provide solution for the above situation.

Please provide appropriate steps for this. The application is in Swing
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
The application might be in Swing, but the entered text is a String. Maybe a regular expression and matcher would be a good next step.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

What you need is a document listener. With it you can get notifications for all the changes in the document and you can execute your validation code there. Heres one example which calls the validate() which will contain the actual validation code.



[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
For a simple TextField, using KeyListener might be a little easier.

Of course, it depends how complicated your validation requires.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by RaviSingh Kumar:
Want to validate the textfield as soon as some invalid text is entered.
for e.g. If is EmpID field, error message is shown as soon as some non-integer value is entered.

I had tried it to validate once the whole form is submitted,but unable to provide solution for the above situation.

Please provide appropriate steps for this. The application is in Swing

This is what JFormattedTextField is for, if you can presume java 1.4 or better. Alas, JFormattedTextField isn't all that well documented.

(Calling DefaultFormatter.setAllowsInvalid(false) would prevent the user from typing invalid input at all, which is a bit different from allowing it but showing an error message.)

[edit: JFormattedTextField came with 1.4, not 1.5]
[ October 19, 2007: Message edited by: Brian Cole ]

bitguru blog
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Originally posted by Brian Cole:
Calling DefaultFormatter.setAllowsInvalid(false) would prevent the user from typing invalid input at all, which is a bit different from allowing it but showing an error message.


I was looking for that one , thanks Brian.


SCJP, SCJD, SCWCD, SCBCD
Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 430
You might want a Numeric TextField


To be or not to be. It's a question.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Why not use a JSpinner?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: Validation for Textfield
 
Similar Threads
Validation in Swing Application
TextField with comma
textfield that accepts only numbers???
JPopUPmenu Problem of PopUp scroll
ajax and struts2