Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restrict input type on a JTextField

Status
Not open for further replies.

Bravogolf

Programmer
Nov 29, 2002
204
GB
Hi all,
I have a textfield with an actionlistener that performs a mathematical calculation, so it will only take number.
If it type in text and press enter, it displays various errors in the compiler (naturally).

How can I insert in the action listener that if the value of the textfield is text then do nothing?
 
'Number' isn't a very clear term in the Java-field.
Here I got some expressions, which might be a number in some context:

Code:
1
-1
-1.
-1,
0xe7
0.333
0,333
,3
.3
7e-24
IX 
O777
101101010(2)
9a7fgz
Sometimes this will work:
Code:
String s = tf.getText ();
int i = 0;
try
{
   i = Integer.parseInt (s);
}
catch (NumberFormatException nfe)
{
   // Messagebox: please try again...
}
For non-ints there are similar methods.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top