2012-06-11 33 views
5

我有這個類我的輸入文本框:如何關注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); 

     } 
+0

另一個問題:我如何引用文本框的值? private InputTextBox newUser = new InputTextBox(「Username」); newUser.getText()不起作用? –

回答

2

使屬性字段,用於文本框,並在setFocus方法調用textBox.setFocus(真),或任何你叫你的文本框屬性。

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 
3

您應該將此塊添加到構造函數或的onLoad方法:

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