Solved

Validating a TextField in an Applet

Posted on 2002-03-21
4
293 Views
Last Modified: 2010-05-18
Hi everyone!
This is an easy question:
How can I validate a TextField to be sure that it contains only numbers? If it has something else I want to Open a Message Box informing the problem and return the focus to it.
This must be done in an Applet.

Thanks for any help in Advance!

Regards,
MRXZ
0
Comment
Question by:mrxz
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 6886232
Add a KeyListener to the TextField and disallow anything that's not part of a number
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6886293
> Add a KeyListener to the TextField and disallow anything that's not part of a number

they'll still be able to paste text into the textbox... to be sure, you can check the contents when they submit (click the button or whatever)

try {
   int textVal;
   textVal = Integer.parseInt("<text in textbox>");
} catch (NumberFormatException nfe) {
   // not an int so do what you need to do
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6886303
That's true bobbit31!
0
 
LVL 5

Accepted Solution

by:
Calron earned 10 total points
ID: 6894111
The way to solve this is to write a Document for the textfield. The concept is explained nicely in the java tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html#validation

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question