Textfield focus() with selectAll() produces FocusListener-Loop


i have a complex table with TextField’s in it.
I want the text in the TextField automatically selected if i click on it or programatically focussed it so the user can write instantly.

i added a FocusListener on the TextField which performs a selectAll() on self - sadly selectAll() will call focus() and the FocusListener will be fired again in spite of its allready focussed. so i get a nice listener loop.

i tried to override the setSelectionRange() to avoid the focus() call but it accesses private members needed for selection so i’m stuck.

Some ideas how to solve this?

I can only come up with some sorta hacks to get around the issue. One possibility is to remove the FocusLlistener just before the selectAll() -call, and add it back straight after. Another is to have a boolean and do something like this…

onFocus ( ... ) { if(!updating) { updating = true; selectAll(); updating = false; } } That would not call then selectAll more than once.

I know that these are not the most pretty solutions but they might be enough in some cases and they might give you some new ideas on what to try.

I had a similar solution with a boolean variable. but this is a highly asynchronous process and i did not feel good with such a hack :wink:
sometimes i strangely run in an endless loop anyway.

i ended up integrating jQuery in vaadin. now its all fine.


im having the same problem, also getting a listener loop. Even with removing listener and adding it after calling selectAll(). Any other possibilities?

Anyone? :frowning:

I was getting the same problem when I executed focus on a TextField when one of my custom components was built, in order for that field to have initial focus when a Window was displayed. After that, if I selected data from outside the browser window, for example, in order to copy and paste data into another TextField, the instant I clicked onto the second field, the infinite focus event loop would start for the first field. I think I was able to get around this problem with a hack where the focus listener was removed from the first TextField before executing selectAll and then was added back on a separate thread. It’s ugly, I know, but, for what it’s worth, here’s what the code looks like:

class TextFieldFocusListener implements FocusListener {
public void focus(FocusEvent event) {
Component component = event.getComponent();

	if (component.equals(firstTextField)) {
		AddListenerBackThread addListenerBackThread = new AddListenerBackThread(firstTextField, this);
	else if (component.equals(secondTextField)) {


class AddListenerBackThread extends Thread {
private TextField textField;
private FocusListener focusListener;

public AddListenerBackThread(TextField textField,  FocusListener focusListener) {
	this.textField = textField;
	this.focusListener = focusListener;

public void run() {



I had the same problem with Vaadin 7. To solve it I created a CustomTextField that extends TextField and I have rewrote the focus method like that:

public void setCallFocus(boolean callFocus){
      this.callFocus = callFocus;
    public void focus() {
        if (callFocus) {

So you can call selectAll like that:
field.callFocus(false);//disallow call of focus method
field.callFocus(true);//reallow call of focus method

It’s the same principle that Jens has explained.

I hope it’s help :)[i]