I have one form (a JPanel ) which has 35 textfields and other components.
My questions are:
Q. How should i write the eventhandling method for each textfield for implementing validation checks on the data entered by user?
Q. Whether should i write actionPerformed() with getSource() method , to check which textfield has been hit or is there any other technique so that my work becomes easier to write and manage?
pankaj saxena wrote: . . . Q. How should i write the eventhandling method for each textfield for implementing validation checks on the data entered by user?
Don't know. It depends on the design. I would have thought you would have a button to accept the text. That would call a method for each text field. You wouldn't want to validate the entries before that, because they might be incomplete.
I would have thought you could use regular expressions for the validation, but some are complicated: the regex for an e-mail address takes about 1 page to write out
pankaj saxena wrote:Q. Whether should i write actionPerformed() with getSource() method , to check which textfield has been hit or is there any other technique so that my work becomes easier to write and manage? . . .j.
The method with getSource for 35 fields would be really difficult to maintain. Separate method for each. Separate Listener for each.
that's why , i am so confused that which approach i must use. But, anyhow I am familier with getSource() method and i have used it for 6 or 7 textfields but this time the textfields are more in numbers . so I will use this but I'll keep searching another good technique for it.
You're confused? Okay, I can understand that. So here's an example of what you need to consider...
You say you have a field which is intended to contain an e-mail address. So I start typing my e-mail address in that field. As soon as I type the letter "p" into the field you get an event from the field. What do you want to do at that point? Do you want to validate the field and tell me that "p" isn't a valid e-mail address because it's missing the @ sign?
Of course you don't. You don't want to validate the field until I have finished typing my e-mail address. And how are you going to know when I have finished? It's like Campbell said: you have a button which says "OK" or "Submit" or something like that which I use to tell the program that I have finished. So when I click that button, only then do you examine the 35 fields and see if they are valid.