aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to format a textfield to accept different data types?!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to format a textfield to accept different data types?!!" Watch "How to format a textfield to accept different data types?!!" New topic
Author

How to format a textfield to accept different data types?!!

Meghna ks
Ranch Hand

Joined: Mar 15, 2001
Posts: 122
Hi people
I have a bean with Labels and textfields together. The data inside the textfields is a Vector of Objects. I need to be able to get the textfield to accept different datatypes depending on other dependencies.How do I get the textfield to accept :
1. A String datatype only
2. An Integer datatype only
3. A Date datat type only
4. An IPAddress formatted value
Thanks
Meghna
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Meghna,
You could use a FocusListener... and when the textfield loses focus you could validate the results... However, you have problems with some of the requirements...

  1. A String datatype only : Well, technically, anything you can actually type into a TextField is a String...

  2. An Integer datatype only : You could use Integer.parseInt()on the String that the TextField holds inside a try..catch block. If a NumberFormatException is thrown, you don't have an int...

  3. A Date datatype only : DateFormat (in java.util) has a method called parse() that takes a String and returns a Date object... if the String will not parse into a Date it throws a ParseException...

  4. An IPAddress formatted value : Basically, your going to have to make sure the String in the TextField is formatted correctly ( either by using StringParser or just taking substrings based on the decimal points in the IP Address )


  5. Personally, I wouldn't make one TextField that did all of this itself... I would make several classes that extend TextField... each that embody one of these behaviors... then you just use the appropriate type of TextField that you need...
    As far as determining if an input is of one of these types and then deciding what type of textfield you need, I think that it will be very hard to do since everything you type in is a String... a String will be returned... check for int... both a Date and IP Address are made up of ints... and both a date and an IP Address can be ints seperated by points... you are going to have problems if you are using just one field to take in all this data... it would be much better to have each field validate a certain type of data, and then use labels to indicate to the user what they should enter into the textfield.
    HTH,
    -Nate
    [This message has been edited by Nathan Pruett (edited March 28, 2001).]


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to format a textfield to accept different data types?!!