post event on client queue

Hi,

I am using vaadin flow. I have a TextField with a focus listener. Somewhere in the code I call focus() to programmatically activate focus on the TextField. When I do focus() programmatically after the onFocus I need to execute some additional code.

I have tried to call focus() and then UI.getCurrent().access(…) to add additional code. The problem is that the code within access is called before the onFocus. There is a way to post some code that run will run after client events? For example with a setTimeout?

Thanks in advance for any help!

Hi,

I’m not sure if I understand exactly your problem, perhaps you can post an example of your code.

The focus is done only after the element is attached, this may be your problem.

If you have onFocus javascript function, you can call a server function at the end of your javascript function. (element.$server.myFunction():wink:

Here an example (ugly but I call focus then a server function):

private final TextField textfield = new TextField("text 1");
    private final TextField textfield2 = new TextField("text 2");
    public FocusView() {

        add(textfield,textfield2);
		// See Focusable.focus()
        textfield2.getElement().getNode()
                .runWhenAttached(ui -> ui.getPage().executeJavaScript(
                        "setTimeout(function(){$0.focus();},0); alert('focus'); $1.$server.myFunction()", textfield2.getElement(), getElement()));

    }

    @ClientCallable
    public void myFunction(){
        textfield.setValue("FOCUS on 1 ");
    }