• Post Reply Bookmark Topic Watch Topic
  • New Topic

textfield event handling  RSS feed

 
pankaj saxena
Ranch Hand
Posts: 47
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir,

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?

thanks.
regards.
pankaj.
 
pankaj saxena
Ranch Hand
Posts: 47
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moreover, i would like to ask another question under the same thread or post.

Case: I have one textfield and i want to put following validation check on this textfield :

1. text should not be integer.
2. text field should not be left blank.
3. user must type only characters.
4. in case of entering 'email ids' , text must include @ between them.

Q: What approach should i use to implement all those validation checks?
Q: Does n't Exception can be used in this case , if yes, then how should i implement this approach into my program?

well i am also searching & experimenting on this issue.

thanks.
pankaj.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
pankaj saxena
Ranch Hand
Posts: 47
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

thanks anyway.
pankaj.
 
Paul Clapham
Sheriff
Posts: 22506
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out InputVerifier
The API docs have a sample code
 
pankaj saxena
Ranch Hand
Posts: 47
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes sir,

I do have one submit button for validating and sending the data to the database.

and , i am following the advice given by both of you. If I find any problem in this process I will definitely consult with you.

thanks.
pankaj.
 
pankaj saxena
Ranch Hand
Posts: 47
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir,

InputVerifier can't solve my problem.

thanks.
 
Abhilash Etikala
Ranch Hand
Posts: 38
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pankaj,

I do have one submit button for validating and sending the data to the database.


So you have a submit button which could trigger an action event where you can perform validation.

As Paul Clapham said...on clicking the Submit button validate those 35 fields .

Regards,
Abhilash
 
pankaj saxena
Ranch Hand
Posts: 47
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir,

I am following your advice to write that validation code in submit button coding.

thanks.
regards.
pankaj.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!