Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

textfield event handling

 
Ranch Hand
Posts: 47
C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74780
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 26971
85
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sir,

InputVerifier can't solve my problem.

thanks.
 
Ranch Hand
Posts: 38
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sir,

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

thanks.
regards.
pankaj.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic