• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
datatypes depending on other dependencies
do you really expect me to design/suggest something
without knowing the dependencies.......
I mean, will you accept it if I suggest one!
You know that the dependencies are driving here....right!
- satya
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've looked through the API, and I can't find anything along the lines of a mask. But this is what you would need.
Here is a general guideline of how to proceed:
Subclass JTextField.
Make the subclass implement the KeyListener interface.
In the keyPressed (or perhaps keyTyped) event, check what key was just pressed.
If your particular subclass of textfield only accepts characters, then allow anything. If it is only integer values, then 'drop' anything except the digits 0-9.
I haven't tried this out, and don't know if it would even work. But 'catching and releasing' is how this sort of input control can be done in VB, which is my background. In that language, setting the character just pressed to null resulted in the keypress being ignored.
p.s. to Madhav: I took the question to be: "Given that other dependencies have determined the allowable input, how do I ensure that only that type of input is allowed."
In this case, the designer of a textbox that only takes integer values does not care about the dependencies at all. A side benefit of object oriented design.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
p.s. to Madhav: I took the question to be: "Given that other dependencies have determined the
allowable input, how do I ensure that only that type of input is allowed."
In this case, the designer of a textbox that only takes integer values does not care about the
dependencies at all. A side benefit of object oriented design.

Thanks Mike. I sure would have read it that way. It was just
that I was answering some qstns and this one rubbed me on the
wrong side.........
regds.
- satya
 
Meghna ks
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mike.
I found the solution for Integer input by writing another class that would accept Strings and format it to integer and then when the application is run, it ensures the user to input only Integers. Thanks a lot for attempting to understand my question. I'm really sorry if I did'nt explain the problem properly
P.S => Madhav : Yes, I went through the qn. and it really seemed to put the people on the other end in dark when I mentioned other dependencies. I'm sorry about that.
Thanks
Meghna
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S => Madhav : Yes, I went through the qn. and it really seemed to put the people on the
other end in dark when I mentioned other dependencies. I'm sorry about that.

No offense, I assume!
Just having a bad start for the day, well I should blame it
on something right!
At javaranch No Sorry, No Thanks.
Good Luck.
- satya
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic