Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Validation for JTextField  RSS feed

 
VijayaMohan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to check only numerical values should be entered into JTExtField
 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The only way i can enforce this is
String s = textfield.getText().trim();
if(s!= null)
{
try
{
Integer i = new Integer(s);
}
catch(NumberFormatException nfe)
{
System.out.println("Not an integer");
}
}

[This message has been edited by Sajee Joseph (edited October 09, 2001).]
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the Sun tutorial for proper validatation of a JTextField:
http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html#validation
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!