Stop a Field from losing Focus

I have a text field that after the user enters a value and attempts to either tab off the field or click another field the entered value is validated. If the entered value does not pass validation then I would like to prevent the focus from changing and popup an error message stating that fact.  I've put code into the text field listener's LoseFocus() to validate then on failure to set the focus back to the text field. This usually results in an endless loop of the "Invalid Value Dialog" poping up.  I would think there's a really elegant and optimal way to do this.  If anyone know's please let me know.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

post ur code
bonk1Author Commented:

// This is JDK 1.1.8

public void focusLost(FocusEvent fe)
  // make sure its the right field
  // to lose focus
   if ( fe.getSource() == ttraceId )
      if ( checkTraceID() )
          ok.setEnabled( true);
         ErrorDialog ed = new ErrorDialog(slitter,"Scan a valid traceID");
// custom class inherited from Dialog. Is Modal and formats the error nicely
At a first guess, it looks like your error dialog is firing another lost focus event on top of the original one from the user (ie. the act of creating the dialog loses the focus from the field again ad infinitum...)


you need to do this:


before setting the focus back into the field to ensure that the event does not fire again.

Perhaps use a flag to make sure the error dialog does not re-appear until the user has gone back in and made a change.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

 place all your checking condition
 inside another if condition

 if( !e.isTemporary()){
       // your error checking code comes here.

 Reason : When you try to open
          a dialog inside  focusLost
          method , Dialog fires
          a temp. focuslost event.

Sankar S.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.