This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTextField Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField Validation" Watch "JTextField Validation" New topic
Author

JTextField Validation

Kaustuv Bhattacharya
Greenhorn

Joined: Jan 18, 2001
Posts: 7
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

Joined: Nov 26, 2001
Posts: 50
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/


Brent Worden
Venkat Reddy
Greenhorn

Joined: Dec 10, 2000
Posts: 5
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.

 
GeeCON Prague 2014
 
subject: JTextField Validation