[java] Cancelable InputVerifier

Status
Not open for further replies.
I'm having some trouble with an InputVerifier on an input form I'm making.

What I want to happen:

Code:
(1) User enters data in a field
(2) User hits 'tab' (or otherwise causes focus to be lost on that field)
(3) Validation routine hits
   (a) if valid, continue
   (b) if not valid
       1. popup message box, wrap the user on the knuckles, tell them to get their act together
       2. beep
       3. when they close the message box, retain focus in the invalidated field
(4) If the user decides that they don't want to submit their entry, hit cancel and all is forgotten/forgiven

What is actually happening is that the verifier fires, invalidates, message box, beep. User: crap, let me do some research first, clicks okay, clicks cancel, and the verifier fires again because clicking on the button causes focus to be lost. The only way to be able to cancel is to clear the bad entry so the verifier doesn't show as invalid. I can't even make a 'clear' button for precisely the same reasons.

One way to fix it, would be to clear the entry when it is invalid, but for usablility purposes, I prefer not to ruin the user's work because of a typo.

I've found so many ways that people recommend doing this kind of stuff but I'm not sure I picked the right way:

Code:
(1) extend PlainDocument
(2) extend DocumentFilter
(3) extend InputVerifier
(4) create a FocusListener/extend FocusAdapter
(5) etc.

I'm trying to keep this implementation simple, but everytime I turn around, it seems I need to add another verifier, filter, listener or something.

Is there a best practices guide for building UI's and wiring stuff together (either a generic one or one focused on Java/Swing? GUI code (other than simply building the UI's) seems to confound me... <old procedural programmer - Pascal, C, ASM>

Any suggestions?
 
ShuggyCoUk":261xgbkv said:
popup message box

Unnngh. A legion of VB coders rise up and bite your ankles \
off

Please don't do that. Much better UI is to highlight the field in some way (icon/red/bolding boxes etc.) along with a tooltip/side note (or some other construct if you aren't WIMP) as to the reason for your ire.

Okay, fair enough... but now I need to figure out where to put the code that:

  • Changes the text or background to red; sets the tooltip to 'invalid data' when it invalidates
  • Changes the text or background to normal; sets the tooltip to '' when it validates

... theoretically, this should NOT be done in the validator, yes? So now I have to make a FocusListener instead to do all this stuff a different way? or in conjunction with the validator?

Gah! all I want to do is ensure that the user doesn't type garbage and do it in a reasonably inoffensive way ... :p
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
swing's inputverifier appears to be built around it not yielding focus at all:

http://download.oracle.com/javase/6/doc ... ifier.html but it looks like it's fine to just always yield focus but still consider it bad:http://stackoverflow.com/questions/3543458/inputverifier-and-multiple-fields

Just have whatever parent control you're using sanity check before allowing submission (or whatever it is that would actually use the values)
 
:grumble:

Okay, so shouldYieldFocus returns true no matter what, but sets the bad field's background to red when not verified and to UIManager.getColor("TextField.background") when verified.

The Cancel button works. The Save button validates the input (again!) before storing the data.

It seems so hackish somehow ... I always feel like I'm doing something 'wrong' when doing stuff in Swing.
 
Status
Not open for further replies.