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
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
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Jun 03, 2000
Posts: 5040
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

Joined: Mar 15, 2001
Posts: 122
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

Joined: Jun 03, 2000
Posts: 5040
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
 
 
subject: How to format a textfield to accept different data types?!!