File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "JTextField" Watch "JTextField" New topic
Author

JTextField

Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
I'm looking for an example of how to limit a JTextField so that you can put a phone number in it. I'm not sure what you call fields like that but I only want to have the user be able to enter a phone number.
Thanks,
Frank
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6914
This sort of field is often known as "formatted" or "validated", depending on whether the checking is done for every keystroke or only when the field is moved out of/submitted.
However, I have almost a personal crusade against such fields. I absolutely hate fields which limit the input to what the programmer or designer thought might be entered, especially if they are mandatory on the form.
I personally believe that thousands of companies are losing e-commerce sales every day, and probably don't even know about it, all because of needless validation on form fields. About half the on-line forms I see have some form of stupid validation :- either they enforce the US-style 3-3-4 phone numbers, require that I select a "state" from a list (or only allow two characters), only allow numbers in the "zip code" and so on.
Lots of Us-designed forms won't even work for a Canadian customer, let alone one from elsewhere in the world.
Whenever you think about putting formatting or validation on an input field - think twice; No. Think three times and take a cold shower. You don't need it. Let the user decide what is a valid address or phone number.
Sorry for the rant.


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Well I still need a formatted JTextField because the users of my software are dumb people (as most users are). I have to force them to enter the correct data because I cannot trust them to enter in the required information. And this is not for a web site as you may have thought from your rant.
Frank
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
Mr. Hale-
OK, assuming that it is a good/necessary idea in this case, check out Sun's Swing tutorial, specifically Creating a Validated Text Field. I've only skimmed it, but it seems to address your problem. Also, the book "Java Pitfalls" by Daconta, Monk and others has a section on this - worth checking out. Note that some things can be validated immediately (e.g. if the user types '@' you can reject it right away) while others can't be validated until the user hits "Enter" or the field loses focus (e.g. if there is only one digit, more are probably needed). Good luck. For extra fun, try to get Mr. Carver to be one of your beta testers when you think you're done.

"I'm not back." - Bill Harding, Twister
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Thank you I think this is just what I need.
Frank
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: JTextField
 
Similar Threads
Install onto phone
validate jTextField in java swing
how to accept strings and values in invoice textfields
Mask JTextField 1.3
phone number