views:

183

answers:

1

I'm trying to develop an input real-time verification on a textfield in a Java applet.

The idea would be to have an input field that, if empty, once the user clicks in it it would show something like "0,00". Once the user starts to press the keys, only numbers should be accepted, and it would start to fill the text like this (imagine I input the numbers: 1,2,3,4,5,6):

"0,01" -> "0,12" -> "1,23" -> "12,34" -> "123,45" -> "1.234,56".

If the field is not empty the user can change the values but there will always be a "," dividing the decimal numbers.

I've been able to allow only numbers to be accepted but how can produce this kind of behavior? I know this may be a very specific question but any links or examples would be much appreciated. Thank you.

A: 

You will have to provide an input handler, that not only filters the input, but also calls a preset callback (made by you), that will update the required field in the way you want it to be updated.

You can use some functions, that can format numbers, given a specific format.

Basically, just keep a count on number of digits, already input, then parse it as a plain integer then multiply it by a power of 10, derived from the format, in your example would be something like 10 raised to the power of (numberOfInputDigits -2).

Rui Martins