*
The moose likes Java in General and the fly likes How to verify input in the JTextField? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to verify input in the JTextField?" Watch "How to verify input in the JTextField?" New topic
Author

How to verify input in the JTextField?

Wheat Garfield
Greenhorn

Joined: May 25, 2004
Posts: 10
Hi, all, I need to verify the input in the JTextField, they could be int, double, string, boolean, etc. How can I do that? Thanks.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Could you please clarify? Are you trying to verify the data type? The value and range? Do you only have one JTextField for different types of input? (Please note that by definition a JTextField data is text; you need to parse the text to get numerical information.)


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Wheat Garfield
Greenhorn

Joined: May 25, 2004
Posts: 10
Ok, there's a dialog, based on the type that I choose, the type could be an int, a decimal, a boolean, etc. based on the type I chose, I need to verify the input that the user enters in the JTextField. Btw, the input in a JTextField is a text instead of a String? Maybe if there is an alternative component that I can use to simplify this?

Thanks
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
I'm stil not exactly clear what you are attempting here.

Are you saying that you choose your data type AND THEN pop up a dialog based on your choice?

If so, then you already know the data type that will be represented by the text in your dialog text fied.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
The getText method returns a String object, which is one of Java's implementations of text. If you want the user to enter an integer, and you want to validate the integer entered, you would have to parse the String returned from the JTextField using a try-catch block which would catch a NumberFormatException if the value you're trying to parse as an int is not in the range of an int.

If that doesn't help clarify, then please post a snippet of pseudocode to show what you're trying to accomplish and maybe we can help from that. If you do post pseudocode or actual code, please use the UBB CODE tags to help with readability.
Wheat Garfield
Greenhorn

Joined: May 25, 2004
Posts: 10
Thanks a lot! I think the last message is what I'm trying to do: the user first chooses the type, then the user enter the value, based on the type (int, decimal, string, boolean, percent, or any other type the user could define with ranges), we will verify the validation of the value just entered.

For example:

Step1: the user chooses a type called LimiteInteger [0~1];
Step2: the user enters 2.0 in the JTextField
Step3: the user pushes the button "Accept"
Step4: Error Message poped up.

Sorry for the confusion!
 
 
subject: How to verify input in the JTextField?
 
Similar Threads
Limit JTextField number of characters
Is there a standard way of creating a text field that requires the input to be an integer?
How to set data format of JTextField ??
Get data from jtext field
Input verifier and JInternalFrame closing