2011-08-01 3 views
3

На моей странице GWT есть TextArea, и я хотел бы, чтобы она имела фокус и чтобы весь текст был выбран только при загрузке этой страницы. Я пробую код ниже, но он не работает вообще. Вы можете мне помочь? БлагодаряИспользуйте selectAll() в GWT TextArea

final TextArea myText = new TextArea(); 
myText.setCharacterWidth(50); 
myText.setVisibleLines(20); 
myText.setText("Just try something"); 
RootPanel.get("textContainer").add(myText); 
myText.setVisible(true); 
myText.setFocus(true); 
myText.selectAll(); 

ответ

5

Документы из TextBox.selectAll() говорят:

This will only work when the widget is attached to the document and not hidden. 

Скорее всего, ваш TextBox еще не подключен к DOM при вызове .selectAll().

Попробуйте использовать Scheduler:

final TextArea myText = new TextArea(); 
myText.setCharacterWidth(50); 
myText.setVisibleLines(20); 
myText.setText("Just try something"); 
RootPanel.get("textContainer").add(myText); 
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() { 
     @Override 
     public void execute() { 
      // your commands here 
      myText.setVisible(true); 
      myText.setFocus(true); 
      myText.selectAll(); 
     } 
}); 
+0

Отлично! Он работает, спасибо! – Arturo

Смежные вопросы