| | |
Restricting User input to Numerical Values in jtextbox
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
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
•
•
•
•
rather new to java
•
•
•
•
I am using the Netbeans JDE in creating a Java GUI.
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 ?"
"How to ask questions the smart way ?"
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 !!!
You can take a look at using a JFormattedTextField with a MaskFormatter also: http://java.sun.com/docs/books/tutor...#maskformatter
•
•
Join Date: Mar 2009
Posts: 2
Reputation:
Solved Threads: 0
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
and it would not set the text as that.
then tried that in the catch statement putting
and the program would terminate.
is there any way i can get around this??
although i tried inputing the line
Java Syntax (Toggle Plain Text)
jTextField2.setText("Currency Converter accepts numbers only.");
and it would not set the text as that.
then tried that in the catch statement putting
Java Syntax (Toggle Plain Text)
System.exit(0);
is there any way i can get around this??
![]() |
Other Threads in the Java Forum
- Previous Thread: Not sure what is wrong in this program
- Next Thread: Prototyping / Drawing GUI Screens Question
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






