Im wondering if its possible to have a bean that is not quite a service that has access to the front isnt a screen but can be shared by screens to create specific UiComponents or shared things in the screens.
I hope this makes sense… is this possible?
Sure, why not:
import io.jmix.ui.UiComponents;
import io.jmix.ui.component.CheckBox;
import org.springframework.stereotype.Component;
@Component
public class MyUiComponentsFactory {
private final UiComponents uiComponents;
public MyUiComponentsFactory(UiComponents uiComponents) {
this.uiComponents = uiComponents;
}
public CheckBox createCheckBox() {
return uiComponents.create(CheckBox.class);
}
}
public class MyScreen extends Screen {
@Autowired
private MyUiComponentsFactory myUiComponentsFactory;
private void whatever() {
CheckBox checkBox = myUiComponentsFactory.createCheckBox();
// ...
UiComponents
is a regular bean, so you can inject it into any other bean.
Looks simple enough. Can this bean work for background tasks and threads? and can it be located on any package?
Yes, but as a singleton it should not have any mutable state.
last question. Why can I not just inject the UIComponents rather than having that setter?
Of course you can, in the example above it’s just constructor injection.
The following will work too:
@Component
public class MyUiComponentsFactory {
@Autowired
private UiComponents uiComponents;
1 Like