I have developed a swing form in which multiple textfields are required to be validated like - Textfieds can contain maximum 10 digits in which only 2 digits after the decimal are allowed (Ex.- 4562.10) Please tell me how to so it by using KeyListner or any other way.. Please help
KeyListener is the worst of all possible choices. The standard way to be notified when a Swing text field is changed is to add a suitable DocumentListener to it, but really you should start by asking yourself whether you want to validate the field while data is being entered into it (harder than you might think if you didn't think of all the ways a user could mess up), or whether you want to wait until the user tells you they are finished. And you should consider whether using a formatted text field would allow you to avoid a lot of ugly coding.
Joined: Feb 14, 2013
Thanks Paul for the reply, You are absolutely right as there is lot of ugly coding with KeyListener, especially when i am having so many fields in the form. But, I i am a newbie to the java, and having problem in getting familiar with the concept of these DocumentListener or jFormattedTextField. One thing for sure that i want to validate the field while data is being entered into it. Please tell me that what should i do. I am ready to learn more and more..
Before you start writing code to validate the data as it's keyed in, then, you should take some time to consider how you're going to implement that. Being a beginner, you might think that throwing up a dialog box when you find the field's contents are invalid is the right thing to do, but it isn't. And remember that the fields start out blank -- do you consider that valid?
As for how to use a DocumentListener, start with the documentation for JTextField. (<== Follow that link.) It shows you a basic example of how to use a DocumentListener.
subject: Validation required in multiple Textfields in java swing