handling focusLost Events

N

Nuggy

Ok, I'm ready to toss this thing across the room. Here's what I'm
trying to accomplish, greatly simplified:

I have a screen with several text fields. I want to validate the
entry in each text field when the user tabs out of it to the next one
(some fields are required). If they enter an invalid entry or leave
it blank, I want to display a pop-up in the form of a
JOptionPane.showMessageDialog to inform them of the problem, then
reset focus to the invalid field.

Here's the problem: fields a, b, and c are all validated this way,
and are sequential in the focus order. In a new screen, if the user
enters an invalid value into field a, field b briefly gets the focus
from the tab key, and in the course of popping up the box and
resetting focus to field a, field b loses this brief focus, gets
validated because of this, and I end up with two and sometimes three
pop-up boxes that I don't want. I am using isTemporary(); without it
I end up in an infinite loop of popups. Here's a greatly simplified
overview of my focusLost function as it stands now:

public void focusLost(FocusEvent fe)
{
if(!fe.isTemporary()) {
if(fe.getsource == fieldA) {
// do validation here
if(valid) { // do nothing, allow to go on
}
else {
fieldA.requestFocus();
JOptionPane.showMessageDialog...;
}
}
if(fe.getsource == fieldB) {
// do validation here
if(valid) { // do nothing, allow to go on
}
else {
fieldB.requestFocus();
JOptionPane.showMessageDialog...;
}
}
// etc etc for more validated fields
}

I also want to NOT perform the focus lost validation if the user
clicks a "clear" button that clears out all the data on the screen.
It won't let the user do this because when you click the button it
validates the field you were in, and if it's not valid you get the
dialog popup.

I've spent two days trying ideas and I'm still plagued by these
"phantom" popups. If anyone has encountered this issue before and has
any ideas, please post a reply if you have the time to help.

Thanks all.
 
P

Paul Lutus

Nuggy said:
Ok, I'm ready to toss this thing across the room. Here's what I'm
trying to accomplish, greatly simplified:

I have a screen with several text fields. I want to validate the
entry in each text field when the user tabs out of it to the next one
(some fields are required). If they enter an invalid entry or leave
it blank, I want to display a pop-up in the form of a
JOptionPane.showMessageDialog to inform them of the problem, then
reset focus to the invalid field.

Here's the problem: fields a, b, and c are all validated this way,
and are sequential in the focus order. In a new screen, if the user
enters an invalid value into field a, field b briefly gets the focus
from the tab key, and in the course of popping up the box and
resetting focus to field a, field b loses this brief focus, gets
validated because of this, and I end up with two and sometimes three
pop-up boxes that I don't want. I am using isTemporary(); without it
I end up in an infinite loop of popups.

Classic deadlock error. Create a flag that prevents this. If you see the
need to create a validation dialog, first set a "busy" flag, then show the
dialog. In your event handler code, first test the flag, if it is set, do
nothing.

Once the dialog is dismissed and the focus has been reset to the previous
field, then clear the flag. This allows the event handlers to function
normally again.
I've spent two days trying ideas and I'm still plagued by these
"phantom" popups.

All you have to do is think it through. Your event handlers think any
focus-lost event is equal to any other. But they are not the same -- some
are initiated by the user, and some by your dialog.
 
C

Chris

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I also want to NOT perform the focus lost validation if the user
clicks a "clear" button that clears out all the data on the screen.
It won't let the user do this because when you click the button it
validates the field you were in, and if it's not valid you get the
dialog popup.

Hello,
I think this part is probably the hardest. Try to consider people
using the keyboard to navigate your program: when they want to get to
the clear button, they have to Tab through all the other fields to
get there. This means they have to validate, just for the user to get
to the clear button. Personally, I would consider just putting a
little marker, which should be fairly non-intrusive, beside each
field. If it invalidates, put a short message (maybe written in red
text or something) that says so. Finally, as long as one or more
fields are invalid, disable the submission button! The user can't go
any further until the fields are valid, and they know which ones are
valid and which aren't. I think this is a much friendlier way of
accomplishing the same result, especially to keyboard users.

If you REALLY want to do this, though, what you'd need to investigate
is the use of FocusEvent.getOppositeComponent(). It'll tell you what
component focus is moving to, in the case of a lost focus event. For
your mouse users, that'll allow them to click the Clear button. For
your keyboard users, well, hopefully the first element in the window
is the first field of the form, and the last element in the window is
the Clear button. That way, assuming they're smart enough, they
should be able to Shift+Tab backwards through form fields that are
already valid, until they wrap around backwards and get to the Clear
button.

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/mbTfwxczzJRavJYRAmZgAKCafWUXTQyQ7xeN/MVSdKM+qbiuswCeISfr
OhiYvqJ+eHo5bsKbYvl4muE=
=QWpQ
-----END PGP SIGNATURE-----
 
B

Bill Dennis

You could also try using a InputVerifier and its .shouldYieldFocus()
method to block the focusLost from ever being called when the data is
invalid.

Bill Dennis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top