5

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

    this.txtListName.setCursorPos(this.txtListName.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.

z00bs
  • 7,518
  • 4
  • 34
  • 53
jax
  • 37,735
  • 57
  • 182
  • 278

2 Answers2

3

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);
Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
2

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?

z00bs
  • 7,518
  • 4
  • 34
  • 53