Restricting User input to Numerical Values in jtextbox

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 2
Reputation: luke42 is an unknown quantity at this point 
Solved Threads: 0
luke42 luke42 is offline Offline
Newbie Poster

Restricting User input to Numerical Values in jtextbox

 
0
  #1
Mar 18th, 2009
Hey everyone.

rather new to java and was wondering on how i would accomplish this.

I am using the Netbeans JDE in creating a Java GUI.
I have completed the whole script but now am just doing some fine tuning in flattening out any errors that could go wrong with the script such as the user inputing a character rather than a numeric value.

As this program is a currency converter is must also allow for the user to input a decimal place, could anyone please explain to me how to use this?

cheers,
luke.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Restricting User input to Numerical Values in jtextbox

 
0
  #2
Mar 18th, 2009
I'm not sure if there is a way to restrict JTextBox to only numeric values. What I would do is have the user input the data, then parse what every they input as a double. When you do that the decimal will be considered part of the number. When you parse, check for a NumberFormatException, if one is thrown the user most likely inputed letters, ask them again for the data.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Restricting User input to Numerical Values in jtextbox

 
0
  #3
Mar 18th, 2009
rather new to java
I am using the Netbeans JDE in creating a Java GUI.
If you are new to Java then do not directly use GUI designers provided in NetBeans, you might get your initial form designing work done faster but when it comes to do some actual coding with these UI components you will find yourself paralyzed. So begining use a minimalistic IDE (eg JCreator LE) which allows you to just compile and run your code and nothing else (not even code completion). And in case you run into any problems or want to look for methods provided by the built-in java classes refer to the javadocs either online or they can be downloaded from here.

Now as far as your problem is concerned, If I am not wrong you must be having some sort of "button" so the user can tell your application to convert whatever value you have in the JTextField to the specified currency, So over there itself you can put a validation check to ensure whether the user has entered a proper numeric value or not. You can just shoot a getText() method on your JTextField and using the matches() method of the String class in combination with regular expressions, to check if a valid input has been specified.
Alternatively you could also implement the KeyListener to check the value in the JTextField as it is typed on the fly. You can learn more about even handling in Java here.
Last edited by stephen84s; Mar 18th, 2009 at 6:47 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Restricting User input to Numerical Values in jtextbox

 
0
  #4
Mar 18th, 2009
Before performing any calculations on the value from the text field just check using a regex whether every character in the text field is either a digit or a period (.) character. You could form the regex so that you can restrict the number of digits before and after the decimal point.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,629
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Restricting User input to Numerical Values in jtextbox

 
0
  #5
Mar 18th, 2009
Try to parse the number as a double. If an exception is thrown clear the text field and request that the user input a valid decimal number.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,506
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Restricting User input to Numerical Values in jtextbox

 
2
  #6
Mar 18th, 2009
You can take a look at using a JFormattedTextField with a MaskFormatter also: http://java.sun.com/docs/books/tutor...#maskformatter
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Restricting User input to Numerical Values in jtextbox

 
0
  #7
Mar 18th, 2009
Originally Posted by BestJewSinceJC View Post
Try to parse the number as a double. If an exception is thrown clear the text field and request that the user input a valid decimal number.
Read the other posts before posting. That is exactly what I said.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 2
Reputation: luke42 is an unknown quantity at this point 
Solved Threads: 0
luke42 luke42 is offline Offline
Newbie Poster

Re: Restricting User input to Numerical Values in jtextbox

 
0
  #8
Mar 19th, 2009
hi everyone, thanks for the responses.i found the easiest way was to do the try and catch statements when parsing the double.

although i tried inputing the line
  1. jTextField2.setText("Currency Converter accepts numbers only.");

and it would not set the text as that.
then tried that in the catch statement putting
  1. System.exit(0);
and the program would terminate.

is there any way i can get around this??
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: Restricting User input to Numerical Values in jtextbox

 
0
  #9
Mar 19th, 2009
Originally Posted by luke42 View Post
is there any way i can get around this??
I presume you are trying to ask the user for input again? You should be able to set the text in the text field, but without seeing your code I don't know why it wouldn't work. Use a while loop to keep asking for input.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC