• 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 verify input in the JTextField?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Wheat Garfield
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
no wonder he is so sad, he hasn't seen this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic