Boris Brudnoy
Boris Brudnoy

Reputation: 2460

Why does deferring TextBox.setFocus(true) work?

In the process of helping speed up user workflow I wanted to set focus to a TextBox. I started by simply adding the line queryBox.setFocus(true); to a view method. This, however, didn't work. What worked was deferring the call:

  Scheduler.get().scheduleDeferred(new ScheduledCommand() {

     @Override
     public void execute() {
        queryBox.setFocus(true);
     }
  });

Why did it work, vs. my first attempt?

Update

From the reference provided by Baadshah below, it seems this is an existing GWT issue, according to which, "Basically, setFocus() just doesn't work unless its wrapped in DeferredCommand most of the time."

Upvotes: 1

Views: 821

Answers (1)

Suresh Atta
Suresh Atta

Reputation: 122008

YES ,queryBox.setFocus(true); wont work if the queryBox not yet attached to DOM. The TextBox needs to be attached to the document before you can focus it.

You are right,the browser does take time to load the DOM although this delay is not visible to us.

We exactly dont know when the load(i mean attaching to document) of textbox was being completed.

So by using schedular we are making our self to wait some time until the rendering finishes,to apply the focus on rendered textbox.

Here is an interested discussion on the same.

Upvotes: 2

Related Questions