Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField Validation

 
Kaustuv Bhattacharya
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. I want to make one JTextField that only allows numeric input.
2. I want to make one JTextField that only allows a-z, 0-9 and
underscore(_) with the first charecter must be a-z.
Please help as soon as possible with example.
 
Brent Worden
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way would be to override the processKeyEvent method of your components. That way you can intercept an typed in characters and check to see if the user typed in a valid character. If the character is invalid, disallow the character and notify the user by any means neccessary. If the character is valid, allow the character by simply calling the super class' processKeyEventMethod.
To ensure the processKeyEvent method is called you may have to explicitly state your components want to process these events. To do this call enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK) on your components.

------------------
Brent Worden
http://www.Brent.Worden.org/
 
Venkat Reddy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaustuv,
Still if u require answer for 1 u can see the following link.
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#validtext
Venkat


Originally posted by Kaustuv Bhattacharya:
1. I want to make one JTextField that only allows numeric input.
2. I want to make one JTextField that only allows a-z, 0-9 and
underscore(_) with the first charecter must be a-z.
Please help as soon as possible with example.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic