开发者

GWT focus on a TextBox it not working

I am trying to focus on a particular list view in a tree, I am using the following code

    this.txtListName.setCursorPos(this.txtListNam开发者_运维技巧e.getText().length());
    this.txtListName.setFocus(true);

The text view has the cursor blinking inside it but when I type a key nothing happens, I have to select the text view again before being able to type.

Why is this happening.

SOLVED

The setting the the focus was done inside a for loop that looped over and created the Tree Items, when I removed it from the for loop it worked.


Could it be that something in your current call stack is taking the focus away after you set it. You could try setting the focus in a timeout:

(new Timer() {
   @Override
   public void run() {
     txtListName.setFocus(true);
   }
}).schedule(0);


I've tried to recreate your problem but the following snippet works for me:

public void onModuleLoad() {
    Tree tree = new Tree();
    final TextBox box = new TextBox();
    box.setText("some content");
    tree.add(box);

    Button btn = new Button("set focus");
    btn.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            box.setCursorPos(box.getText().length());
            box.setFocus(true);
        }
    });

    RootPanel.get().add(tree);
    RootPanel.get().add(btn);
}

Isn't that what you're trying to achieve?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜