File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField" Watch "JTextField" New topic


jacq carballo
Ranch Hand

Joined: Feb 10, 2002
Posts: 42
Is there a way to validate the data in the JTextField. I only want the user to input numeric values. But i want to be able to trap the key as soon as the key is released. If it is numeric, then it is displayed on the screen. otherwise, whatever is typed is not displayed on the screen anymore. TIA
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
add a TextListener for the textField.
then override the method textValueChanged(TextEvent e).
check for the value that changed. if it is a number continue, otherwise use the consume method to do nothing.
hope it helps.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Actualy with JTextField you use Documents. Here is a link to the the tutorial on JTextField.
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
sorry. you are right Paul.
missed the J

thanks for correcting me.
Don't get me started about those stupid light bulbs.
subject: JTextField
Similar Threads
JTextField Validation
virtual key board source code
Tab Key Binding JTextField
image in glassPane
JTextField on getText() method