Puce - 2006-09-06

Logged In: YES
user_id=329344

Hi John,

as far as I could figure it out it currently works like this:
on each input AbstractXNumberFormatter.stringToValue gets
called (not in my code! I think the place is
DefaultFormatter.updateValue).

stringToValue tries to convert the text of the text field
into a number which is in a specified range. It does not
alter the text of the text field which is good, because the
text is not allways a valid number while editing.

When the focus is lost, the text of the text field changes.
As far as I could see this happens in
JFormattedTextField.processFocusEvent. You can modify it's
behavior by setting the focusLostBehavior property of the
JFormattedTextField. The default is COMMIT_OR_REVERT. You
could change it to COMMIT but then you end up with an
inconsistent field if the text couldn't be converted to a
number. (eg. 4s could be converted to 4 but s4 can't be
converted. The field would still display s4 but I think
getValue would return the old value, the last successfully
parsed value or throw an exception (not sure on this one)).
But also setting focusLostBehavior property to COMMIT would
not notify you of a bad entry by default. And you wouldn't
detect if the value has been modified so it stays in range
(because this happens in my code:
AbstractXNumberFormatter.stringToValue).

Unfortunatly I have currently no time to implement the
notification you would like. But maybe you could do it
yourself. Just download the source code - it's all there.
:-) I think you would have to start with:
AbstractXNumberFormatter.stringToValue
and
override JFormattedTextField.processFocusEvent in
AbstractNumberField

I suggest to add an event for this (or maybe two events: one
for bad entry and one for 'out of range' entry) and let
listeners to register for this event. Then you could show
your message box inside the listener and reset the focus to
the field.

If you have questions about my source, I will try to help
you, of course!

-Puce