Set item for ComboBox at startup

Hi, I have a Spring project running Vaadin 24.1 and I want to initialize a ComboBox with one available item. I use the @PostConstruct Annotation to initialise the Components and the UI. However, if I want to set the item for the ComboBox, I use this code getUI().ifPresent(ui -> ui.access(() -> comboBox.setValue(item))) What I’ve seen is that the UI is not ready at this point. Does anyone know if there is an Interface or other Annoation I can use to actually set the Item in the UI other than the constructor (haven’t tried it yet but would prefer something else) ? That would be great, thanks!

Set item for ComboBox at start

Set item for ComboBox at startup

Why don’t you set the value after you created the ComboBox?

Ok, interesting. If I set it after it is created directly in my View Class (I use MVP Pattern) it works. But if I try to set it from my Presenter with getUI().ifPresent(ui -> ui.access(() -> comboBox.setValue(item))) it does not work.
The presenter would even call the function after my approach to set it after creation.

Edit: If I don’t call it with getUI().ifPresent(ui -> ui.access(() it works. But why?

Sounds like over-engineering and your presenter probably has no access to the UI / other thread?

If I call getUI().isPresent() in the View Class it’s false.

When?

The constructor?

getUI needs the component to be attached

No, after I set the ComboBox and everything up (basically my init(). I search for available files and try to set it.

Ah, okay. Is there a listener or event for that?

The question stands: why are you even using the UI and asynchronous access to just set a value to your combobox?

If you really wanna do that, you can go with multiple hooks like onAttach, beforeEnter or afterNavigation depending on your needs and architecture.

Because the View does and should not know what to display (as far as I understand the pattern). So I get the files and set the value for the combobox from my presenter. But maybe that is a bad idea?
I’ll look into the hooks you mentioned, thanks!

Hard to say, I just add a really old but still valid article :sweat_smile: https://vaadin.com/blog/is-mvp-a-best-practice-

Yeah, I know this article. If I would start from scratch I would not use this pattern but it’s too late now :smile:

You have different MVP patterns but usually my presenter does not have ( or a very limited amount) Vaadin imports. The presenter will get the value and ask the view to display it ( setValue in the view combobox).

So I would not use getUI() ( that’s a view class) in the presenter.

( and definitely no UI#access() )

No my presenter calls a function of the View (e.g. setItem()) to set the item. It was just my mistake to call the getUi in the View class.