setFocus for TextBox does not work?

2,199 views
Skip to first unread message

Magnus

unread,
Nov 21, 2010, 4:52:53 AM11/21/10
to Google Web Toolkit
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

Thomas Broyer

unread,
Nov 21, 2010, 9:16:42 AM11/21/10
to Google Web Toolkit
You might have to call setFocus from a Scheduler.scheduleDeferred
command:
http://code.google.com/p/google-web-toolkit/issues/detail?id=1849

Jeff Schwartz

unread,
Nov 21, 2010, 10:39:46 AM11/21/10
to google-we...@googlegroups.com
The simplest way is to create a public method in the dialog box and call it after the dialog box is created and rendered to set the focus to the user field.


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
Jeff Schwartz

steveb

unread,
Nov 21, 2010, 4:05:02 PM11/21/10
to Google Web Toolkit
I found that using a Timer works. I don't understand why but it does
the job.

Nick Newman

unread,
Nov 21, 2010, 4:13:49 PM11/21/10
to google-we...@googlegroups.com
I think in similar situations I've found that the text box has not actually been displayed on the page (that is, not attached to the root panel) at the time that I ask for it to receive focus. Perhaps that was the case with you?  Might be worth checking.

Nick

Subhrajyoti Moitra

unread,
Nov 21, 2010, 4:41:03 PM11/21/10
to google-we...@googlegroups.com
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

John LaBanca

unread,
Nov 21, 2010, 8:47:46 PM11/21/10
to google-we...@googlegroups.com
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
jlab...@google.com

Magnus

unread,
Nov 21, 2010, 11:20:09 PM11/21/10
to Google Web Toolkit
Hi,

thank you very much, these explanations are great.

But it still does not work! :-/

I found that I never explicitely attach the DialogBox to the
RootPanel. Remember, it is of class DialogBox, which appears right
after creating it and calling "center". So I always concluded that the
attachment is done in the background somehow.

If I explicitely attached it to the RootPanel, I would think that this
would then be done twice, because the DialogBox is shown after
"center"...

What I acutally did is to create the deferred command as recommended
(see code below).

What do you think?

Thanks
Magnus

-----

public class LoginBox extends DialogBox
{
private final TextBox txt_User = new TextBox ();
private final PasswordTextBox txt_Password = new PasswordTextBox ();
// "Field" is just a VerticalPanel combining the label with the text
box
private final Field fld_User = new Field ("User",txt_User);
private final Field fld_Password = new Field
("Password",txt_Password);

public LoginBox ()
{
build ();
}

public void build ()
{
setText ("Login");
setGlassEnabled(true);

// arrange the fields using a table ...

PushButton btn = new PushButton ...
}

public void run ()
{
center ();

Scheduler.get().scheduleDeferred
(
new ScheduledCommand()
{
@Override
public void execute()
{
fld_User.setFocus();
txt_User.setFocus(true);
}
}
);

}
}

Evan Cheng

unread,
Jul 20, 2011, 8:38:21 PM7/20/11
to google-we...@googlegroups.com
I think you need to add ScheduledCommand after widget onLoad().

public void onLoad() {
    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
        public void execute() {
            txt_User.setFocus(true);
        }
    });
}

Reply all
Reply to author
Forward
0 new messages