user411103
user411103

Reputation:

Use selectAll() in a GWT TextArea

My GWT page has a TextArea and I would like it to have the focus and to have all the text selected just when this page is loaded. I try the code below but it does not work at all. Can you help me? Thanks

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

Upvotes: 3

Views: 1487

Answers (1)

Peter Knego
Peter Knego

Reputation: 80340

The docs of TextBox.selectAll() says:

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

Most probably your TextBox is not yet attached to DOM when you call .selectAll().

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

Upvotes: 4

Related Questions