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.
bonk1Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
vladi21Commented:
post ur code
0
 
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);
      }
      else
      {
         ErrorDialog ed = new ErrorDialog(slitter,"Scan a valid traceID");
// custom class inherited from Dialog. Is Modal and formats the error nicely
                        ed.setVisible(true);
      ed.dispose();
      ttraceId.requestFocus();
      }
   }
}
0
 
JodCommented:
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...)

OR

you need to do this:

fe.consume()

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.
0
 
sankars98Commented:

 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.
0
All Courses

From novice to tech pro — start learning today.