05-13-2009 07:40 AM
Unlike most other languages we work in, Java on the Blackberry does not appear to have an input field which will accept an input mask.
We're used to being able to set a property of a field to an input mask like "(###) ###-###" or if part numbers require an Upper case character followed by a lower case character followed by a dash and three numbers, then "Xx-###" - the nature of the mask string depending on the specifics of the langugage but apparently, this environment has no such thing.
Ok, fine, then we were looking for a "validate" method that fired BEFORE the unFocus method of a text field or an edit field so that we could WRITE a validate against a mask, but again, no such thing that I can see.
Over and over I see replies that say "Learn some basic java, learn some Swing don't ask Newb questions." But SWING has JFormattedTextField, and apparently Blackberry ui doesn't have an equivalent.
On a different thread I saw a clue as to how to WRITE a FormattedTextField....
Looking at it I think the logical place to deal with it is in keyChar() isn't it? Then I can check the length of the getText() to date, compare the incoming character against the appropriate character of the mask, and if it's good, call super.keyChar() and if it's not, return false.
Am I on the right track here, or is there a "standard" way to do this that I'm overlooking? I _hate_ reinventing the wheel over and over.
05-13-2009 08:16 AM
05-13-2009 08:48 AM
Reduced keyboards is why I thought keyChar() instead of whatever key PRESS is, since presumably the character is settled by the time it's called.
uh if keyChar() isn't good for reduced keyboards, then what _does_ make sense? what is consistantly called before unFocus that could be used as a field-valid method?
-- that question is presuming, of course, that unFocus can't be stopped, which may be wrong. If I can say "no, wait, don't unFocus this, it isn't valid" then unFocus would work fine, but in my reading, I don't see a way to do that. If I can "just" refturn false from unFocus if the field isn't valid and call super.unFocus if it is, then that works great.
But it seems unlikely given the reading I've been doing.
05-13-2009 05:35 PM
In my experience keyChar is not invoked in a sensible way with reduced keyboards (including the Storm) so I wouldn't use it. I've ended up using a FieldChanged Listener (making sure it wasn't called recursively when I reset the Field if it was incorrect).
And I believe you can use the onUnfocus and I think I've done a setFocus in it, though I wasn't comfortable about doing so. I suspect the 'proper' way to do this is use invokeLater to (a) display a Dialog box explaining the problem and (b) do a setFocus to get Focus back on the Field you want. At least I think that should work.
05-13-2009 06:30 PM
Thanks. I agree that a field-changed listener is the best way to go for character-by-character validation.
for a "whole field validate", unFocus and doing a setFocus makes sense.
The other thing we were considering was using navigationMovement, doing the whole-field validate there, and if it was invalid, set dx and dy to 0, and calling super.navigationMovement, which should prevent unFocus from firing.....
All in all, the absence of a "Validate" method which fires before unFocus is just "odd" to me, every other language I've done object oriented screen builders in had one, return true if valid, return false if not. Simple dimple. this willy-nilly pick and choose which method you want to cram a field-valid into seems odd at best.
Still, I suppose I can build a library of "myEditField", "myDateField" etc, which have a valid method, and which have code in the "...something..." method which CALLS the valid method.
and then, we just do what we want in Valid for this instance, and return true or false, and it should work.
Anyway, thanks for listening. I appreciate it. After 9 years of working in windows and on Palm OS, the blackberry is stretching my aging brains ability to form new pathways. That's probably good for me.
05-13-2009 07:42 PM
Oppsss. I forgot to mention TextFilter. I think you might be able to use it. To be honest, I've looked at doing things using a TextFilter but found it not very clear. But I'm not the sharpest tool. So I would encourage you to have a look at it.
I've just implemented, for a 3rd party, a mask validation checking function (*99 means ends in 2 digits for example). This is only used to verify the Fields once the Screen has been finished, as part of the 'Next' processing, so I didn't have the need to implement this in things like Field onUnfocus. However I'm thinking I might do it if it is easy, so your findings could be useful to me.
Of course you also have to do it at Screen completion in case the Field never gets focus - and remember wit the Storm focus doesn't just go up and down....
05-13-2009 08:42 PM
oh heck, I'm using a grid manager anyway. focus doesn't just go up and down... :-)
that's why I was thinking about using navigationMovement instead.
As to textFilter, if there is a technique for textfilter to be overridden and ___ KNOW WHICH CHARACTER IT IS ON ___ I haven't figured it out yet. :-)
Perhaps it can, I just haven't figured it out yet.
If we do either of these I'm going to flat out POST THEM. No one should ever HAVE to write this again. Jeeeez.
05-14-2009 04:32 AM
One other thing, I'm not sure how navigatinMovement works in a Storm, it is soemthing I've never seriously looked at. If you are targetting a Storm, you might like to give it quick test. I know I have had problems with navigationClick on a Storm.
05-14-2009 08:56 AM
Well..... it turns out that we're going elsewhere anyway.
But you gave us the clues we needed to make it work.
I'll be starting a different thread when we post a solution.
07-20-2010 01:02 PM
What did you come up with here? I have beginnings of solutions revolving around overriding keyChar, but, nothing solid yet. The Filters seem to be quite limited in their ability. The phone number filter seems to simply allow the user to enter numbers. I would like if possible to do somethign like put a template up for the user like this:
"( ) - "
And let the user type a phone number in. Would be nice to be able to validate that the user actually entered 10 digits in as well.