473,378 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

handling focusLost Events

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.
Jul 17 '05 #1
3 7841
Nuggy wrote:
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.

--
Paul Lutus
http://www.arachnoid.com

Jul 17 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nuggy wrote:
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-----
Jul 17 '05 #3
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
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eric Newton | last post by:
VB's more declarative nature of handling events is golden. I'm hoping C# will acquire this type of deal, in addition to the anonymous delegates. could do same as vb (actually would be easier to...
4
by: hillcountry74 | last post by:
Hi, I'm a newbie and trying to understand event handling in c#. I have understood handling events using delelgate objects. But not this method- "Event handling by overriding the virtual...
3
by: Ashok Kumar K | last post by:
Hi all, Where can I get some insight on using the __hook, __unhook, event_source and event_receiver for specifically COM events. The documentation given in MSDN is very minimal. I have the...
12
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown...
9
by: Sridhar | last post by:
Hi, I have created a web page which includes a place holder. I also have a dropdown list in that webpage. when I select one of the choices in that dropdown list, It will load a user control...
2
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
2
by: Jordan | last post by:
I need to handle UI events in a worker thread instead of the primary UI thread. In C#, is the normal UI event handling behavior to run in a context thread on the thread pool or are events always...
1
by: bashanmu | last post by:
Hi, i have text field with min.value = 1 & max.value = 100. whenever i give out of range value and click on anywhere in the form, its throing the error "Out of range". It's perfectly ok. but if i...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.