如何关注GWT中的元素?

时间:2012-06-11 00:09:08

标签: java gwt

我的输入文本框有这个类:

    class InputTextBox extends FlowPanel { 
      public InputTextBox(String labelText) { 
        super(); 
        Label label = new Label(labelText); 
        TextBox input = new TextBox(); 
        this.add(label); 
        this.add(input); 
        this.addStyleName("myBox"); 
      }


    }

如何在该文本框上设置焦点,以便在调用onmoduleload时,光标出现在文本框中?添加成员函数似乎会引发许多错误。

      public void setFocus(boolean b) {
        this.setFocus(b);

      } 

3 个答案:

答案 0 :(得分:3)

您应该将此块添加到构造函数或onLoad方法:

Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
@Override
public void execute() {
    //call setFocus method here:
    input.setFocus(true);
}});

答案 1 :(得分:2)

为您创建一个属性字段TextBox,并在您的setFocus方法中调用 textBox.setFocus(true),或者您调用TextBox属性的任何内容。

答案 2 :(得分:0)

像这样更改你的代码

class InputTextBox extends FlowPanel {
    private Label label;
    private TextBox textBox;

    public InputTextBox(String labelText) { 
        super(); 
        label = new Label(labelText); 
        textBox = new TextBox(); 
        this.add(label); 
        this.add(input); 
        this.addStyleName("myBox"); 
    }

    public void setFocus(boolean focus) {
        textBox.setFocus(focus);
    }

    public String getText() {
        return textBox.getText();
    }
}

像这样使用

private InputTextBox newUser = new InputTextBox("Username");
newUser.setFocus(true); // Set focus
String value = newUser.getText(); // get text