The TextBox needs to be attached to the page before you can focus it.
So, the following *should* fail:
TextBox textBox = new TextBox();
textBox.setFocus(true); // Fail: not attached to the DOM
RootPanel.get().add(textBox);
And the following *should* work:
TextBox textBox = new TextBox();
RootPanel.get().add(textBox);
textBox.setFocus(true); // Works: attached to the DOM
The reason a deferred command works is because the deferred command fires
after the current event loop, which means that your text box gets attached.
TextBox textBox = new TextBox();
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
textBox.setFocus(true);
}
});
RootPanel.get().add(textBox); // Fires before setFocus
Notice that I said it *should* work. In reality, focus can behave very
weirdly, especially in IE. I've found that if you create an element and try
to focus it in the same event loop, the focus often fails in IE. If your
TextBox was already created (say in a constructor), it should be focusable
as soon as you attach it to the DOM. In either case, using a deferred
command should always work.
We should be able to just make all calls to focus use a deferred command in
IE, but then again maybe not. What if you call focus() then blur()
synchronously? That means blur also needs to be in a deferred command. I
think it still works, but we'll have to test all the use cases to make sure.
Thanks,
John LaBanca
[email protected]
On Sun, Nov 21, 2010 at 4:41 PM, Subhrajyoti Moitra
<[email protected]>wrote:
> Use a DeferredCommand or a ScheduledCommand (GWT2.1)
>
>
> TextBox textBox=new TextBox();
> Scheduler.get().scheduleDeferred(new ScheduledCommand() {
> @Override
> public void execute() {
> textBox.setFocus(true);
> }
> });
>
> I think u might have already visited this link..
>
> http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDelayed.html
>
>
> Thanks,
> Subhro
>
>
> On Mon, Nov 22, 2010 at 2:35 AM, steveb <[email protected]>wrote:
>
>> I found that using a Timer works. I don't understand why but it does
>> the job.
>>
>> On Nov 21, 8:52 pm, Magnus <[email protected]> wrote:
>> > Hi,
>> >
>> > I am using a DialogBox as a login box and I would like the "user"
>> > field to get the focus automatically.
>> >
>> > I tried txt_User.setFocus (true), but without success.
>> >
>> > How can I do that?
>> >
>> > Magnus
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Google Web Toolkit" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<google-web-toolkit%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/google-web-toolkit?hl=en.
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-web-toolkit%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-web-toolkit?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.