UI.getCurrent().add(xxxx); Not working in Vaadin 11

My code is:

HorizontalLayout frame = new HorizontalLayout();
frame.setId("frame");
UI.getCurrent().add(frame);

Pls. help)

Hi, could you post the whole class so that the problem would be clearer?
Also, could you elaborate on Not working? Is it throwing an exception, missing from the web app page or anything else?

Same issue here.

https://github.com/vaadin/flow/issues/4690

Work around:

UI.getCurrent().getSession().access(()->{
System.out.println("command via session");
HasComponents context = UI.getCurrent();
context.add(new Label("ADD TO UI VIA SESSION EXECUTE"));
});

This implies that ThreadLocal fetch of the UI while in the main view isn’t working correctly or something.

The call UI.getCurrent().add(…) seems to work fine in “onAttach”. It appears you can’t get the current UI while inside a view’s constructor. Presuming because the view hasn’t been initialized and attached yet.

I’ve decided this probably isn’t a bug so much as a documentation issue.
While in the view’s constructor the view hasn’t been attached yet.
Any previous content seems to get removed before onAttach of the current view, hence, any use of UI.getCurrent().add(…), while successful, the components get removed immediately at the onAttach() phase just before the view’s onAttach notification call.