2012-06-11 10 views
5

mam tej klasy dla mojego wejścia tekstowego:Jak skupić się na elemencie w 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"); 
     } 


    } 

Jak ustawić ostrość na tym polu tekstowym więc kiedy onmoduleload nazywa kursor pojawia się w polu tekstowym? Dodanie funkcji członka wydaje się powodować wiele błędów.

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

     } 
+0

kolejne pytanie: w jaki sposób podaj wartość pola tekstowego? private InputTextBox newUser = new InputTextBox ("Nazwa użytkownika"); newUser.getText() nie działa? –

Odpowiedz

2

Zrób dziedzinie własności dla Ciebie i TextBox w setFocus wywołanie metody textBox.setFocus (true), czy cokolwiek zwanego aĹ tekstowym.

0

Zmień swój kod jak tak

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(); 
    } 
} 

Używaj go jak tak

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

Należy dodać ten blok do konstruktora lub metoda onLoad:

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