无法在 GWT 应用程序中将焦点设置在 TextBox 上

发布于 2024-11-06 06:04:56 字数 809 浏览 1 评论 0原文

这不应该给我带来如此多的痛苦,但事实确实如此。这是一个非常奇怪的问题。在 GWT 应用程序中,我有两个 .java 文件:login.java 和 application.java。 在login.java 中,我正在创建一个用户登录页面,如果用户名和密码经过验证,则用户将登录到应用程序,application.java 将从此处获取。

现在正在申请中。 java 的 onModuleLoad() 这就是我从登录页面开始的方式。

public void onModuleLoad() {
  Login login = new Login();
  login.textBoxUsername.setFocus(true);
  RootLayoutPanel.get().add(login);}

除了页面加载时无法将焦点设置在用户名 TextBox 上的小问题之外,这非常有效。我已经尝试了我能想到的一切。但焦点并未设置在 TextBox 上。如果有人可以提出解决方案,请这样做。非常感谢您的帮助。

解决方案:(如果它可以帮助任何面临同样问题的人)

final Login login = new Login();
  Scheduler.get().scheduleDeferred(new ScheduledCommand() {
        public void execute () {
            login.textBoxUsername.setFocus(true);
        }
   });

  RootLayoutPanel.get().add(login);

This should not be causing me so much pain but it is. It is a very weird problem. In a GWT application, I have two .java files, login.java and application.java.
In login.java, I'm creating a user login page where if the username and password is verified the user is logged into the application and application.java takes from here.

Now in application. java's onModuleLoad() this is how i'm starting with a login page.

public void onModuleLoad() {
  Login login = new Login();
  login.textBoxUsername.setFocus(true);
  RootLayoutPanel.get().add(login);}

This works great, except for the tiny problem of not being able to set focus on the username TextBox when the page loads. I have tried evrything I can think of. But the focus just doesn't get set on the TextBox. If anyone can suggest a solution, please do. Your help is greatly appreciated.

Solution: (In case it helps anyone facing the same issue)

final Login login = new Login();
  Scheduler.get().scheduleDeferred(new ScheduledCommand() {
        public void execute () {
            login.textBoxUsername.setFocus(true);
        }
   });

  RootLayoutPanel.get().add(login);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赠我空喜 2024-11-13 06:04:56

尝试使用 Scheduler.scheduleDeferred()

public void onModuleLoad() {
    Login login = new Login();
    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand () {
        public void execute () {
            login.textBoxUsername.setFocus(true);
        }
    });
    RootLayoutPanel.get().add(login);       
}

更新:答案已更新使用 Scheduler.get().scheduleDeferred() 而不是 DeferredCommand,已弃用。

Try using Scheduler.scheduleDeferred():

public void onModuleLoad() {
    Login login = new Login();
    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand () {
        public void execute () {
            login.textBoxUsername.setFocus(true);
        }
    });
    RootLayoutPanel.get().add(login);       
}

Update: answer updated to use Scheduler.get().scheduleDeferred() instead of DeferredCommand, which is deprecated.

星軌x 2024-11-13 06:04:56

为什么使用DefferedCommand,我认为最好使用someWidget.getElement().focus(),它是原生的Javascript。我到处都在使用它,没有发现任何问题。

Why using DefferedCommand, I think it's better to use someWidget.getElement().focus() which is a native Javascript. I'm using it everywhere, I've not seen any problem.

玉环 2024-11-13 06:04:56

如果您的 Widget 扩展了 Composite,您可以:

@Override
protected void onAttach() {
    super.onAttach();
    textBoxUsername.setFocus(true);
}

If your Widget extends Composite, you can:

@Override
protected void onAttach() {
    super.onAttach();
    textBoxUsername.setFocus(true);
}
烟凡古楼 2024-11-13 06:04:56

GWT 可以很容易地在内部状态中存储“wantsFocus”,并在附加小部件后调用焦点。然而,多年后我们仍在等待该功能...

尽管如此,即使在调用附加处理程序之后,setFocus 也并不总是有效。

因此,与此同时,我们的 GwtUtil 库使用了以下代码。它是其他几种解决方案的组合,并被包装在一个实用函数中:

static public void setFocus(final FocusWidget focusWidget) {
    if (focusWidget.isAttached()) {
        Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
            @Override
            public void execute() {
                focusWidget.setFocus(true);
            }
        });
    } else {
        focusWidget.addAttachHandler(new AttachEvent.Handler() {
            @Override
            public void onAttachOrDetach(AttachEvent event) {
                if (event.isAttached()) {
                    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
                        @Override
                        public void execute() {
                            focusWidget.setFocus(true);
                        }
                    });
                }
            }
        });
    }
}

并像这样调用它:

setFocus(myTextBox);

使用实用函数是有意义的;如果 GWT 最终使 setFocus 工作,您将不必在多个位置更改源代码。

It would be so easy for GWT to store a 'wantsFocus' in the internal state, and call focus after the widget is attached. We are still waiting after many years for that feature however...

Still, even after the attach handler is called, setFocus does not always work.

So in the meantime, our GwtUtil library has used the following code. It is a combination of several other solutions, and has been wrapped in a utility function:

static public void setFocus(final FocusWidget focusWidget) {
    if (focusWidget.isAttached()) {
        Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
            @Override
            public void execute() {
                focusWidget.setFocus(true);
            }
        });
    } else {
        focusWidget.addAttachHandler(new AttachEvent.Handler() {
            @Override
            public void onAttachOrDetach(AttachEvent event) {
                if (event.isAttached()) {
                    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
                        @Override
                        public void execute() {
                            focusWidget.setFocus(true);
                        }
                    });
                }
            }
        });
    }
}

And call it like this:

setFocus(myTextBox);

It makes sense to use a utility function; If and when GWT finally makes setFocus work, you won't have to change your source code in multiple places.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文