Focus on window.addCloseListener


I want to focus on TEXTFIELD name when I close the window, but the focus always stays on the last element that took it, before opening the window. am I making a mistake?

private VerticalLayout layout = new VerticalLayout();
private Window window= new Window("Titulo");
private TextField name = new TextField();

@Override    
protected void init(VaadinRequest vaadinRequest) {        

    Button button = new Button("Open Window");
    button.addClickListener( e -> {
        window.setContent(new Label("Test!!!!!"));
        window.setModal(true);
        UI.getCurrent().addWindow(window);
        window.addCloseListener(b -> name.focus());
    });
    
    layout.addComponents(name, button);        
    setContent(layout);

See https://github.com/vaadin/framework/issues/5891

I worked around it with

[code]
public class ForceFocus implements BlurListener
{
private Registration registration;
private TextField field;

public ForceFocus(TextField field)
{
this.field = field;
registration = field.addBlurListener(this);
field.focus();
}

@Override public void blur(BlurEvent event)
{
field.focus();
registration.remove();
}

public static void on(TextField field)
{
new ForceFocus(field);
}

}
[/code](original idea from the comments)

and doing a ForceFocus.on(myComponent) at the end of the close-listener

it worked perfectly :slight_smile:
Thanks.

Nicklas Karlsson:
See https://github.com/vaadin/framework/issues/5891

I worked around it with

public class ForceFocus implements BlurListener
{
  private Registration registration;
  private TextField field;

  public ForceFocus(TextField field)
  {
    this.field = field;
    registration = field.addBlurListener(this);
    field.focus();
  }

  @Override public void blur(BlurEvent event)
  {
    field.focus();
    registration.remove();
  }

  public static void on(TextField field)
  {
    new ForceFocus(field);
  }

}

(original idea from the comments)

and doing a ForceFocus.on(myComponent) at the end of the close-listener

Hi! I’m trying to implement this class but i’m working with Vaadin 7 and the interface Registration starts on Vaadin 8.

Now i’m looking for equivalent class or interface for Registration but i haven’t luck, any solution or idea for this problem?
Thanks!

Diego Sept:

Nicklas Karlsson:
See https://github.com/vaadin/framework/issues/5891

[I]
(https://www.dqfansurvey.website/) worked around it with

public class ForceFocus implements BlurListener
{
  private Registration registration;
  private TextField field;

  public ForceFocus(TextField field)
  {
    this.field = field;
    registration = field.addBlurListener(this);
    field.focus();
  }

  @Override public void blur(BlurEvent event)
  {
    field.focus();
    registration.remove();
  }

  public static void on(TextField field)
  {
    new ForceFocus(field);
  }

}

(original idea from the comments)

and doing a ForceFocus.on(myComponent) at the end of the close-listener

Hi! I’m trying to implement this class but i’m working with Vaadin 7 and the interface Registration starts on Vaadin 8.

Now i’m looking for equivalent class or interface for Registration but i haven’t luck, any solution or idea for this problem?
Thanks!

Thank you for the explanation. It is clear for me.