Link to home
Start Free TrialLog in
Avatar of bonk1
bonk1

asked on

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.
Avatar of vladi21
vladi21

post ur code
Avatar of bonk1

ASKER


// 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();
      }
   }
}
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.
ASKER CERTIFIED SOLUTION
Avatar of shaveri
shaveri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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.