Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Meghna ks
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic