Jmix 2.0 data navigation

Hi,

I had created one combo box in the menuview screen for some users when menuview gets load on the by the login screen screen dialog box first appears on the screen user select value from the dialog box and the selected value is displayed on the combo box created on the right side of the screen.
but in some scenario if particular submenu is clicked the dialog box appear on the screen if any value is selected it must reflected in the combo box created in the menuview screen on the top right side.
How to pass value from any screen to the menuview screen and combo box must be refresh the updated set value.

Hi!

There are two ways to solve your problem.

First way

You can create a spring bean and put some callback in it as a field that will accept new parameters for your ComboBox component.
For example:

@Component("myapp_ComboBoxBinder")
public class ComboBoxBinder {

    private Consumer<List<String>> parametersConsumer;

    public void setParameters(Consumer<List<String>> parametersConsumer) {
        this.parametersConsumer = parametersConsumer;
    }

    public void updateParameters(List<String> parameters) {
        if (parametersConsumer != null) {
            parametersConsumer.accept(parameters);
        }
    }
}

Next, you need to set this callback in your MainView:

    @Autowired
    private ComboBoxBinder comboBoxBinder;
    
    @ViewComponent
    private ComboBox<String> myComboBox;

    @Subscribe
    public void onInit(InitEvent event) {
        comboBoxBinder.setParametersConsumer(this::setComboBoxValues);
    }
    
    public void setComboBoxValues(List<String> values) {
        myComboBox.setItems(values);
    }

And then, just use the installation of a new value through the binder where you need it:

    @Autowired
    private ComboBoxBinder comboBoxBinder;
    
    public void someMethod() {
        comboBoxBinder.updateParameters(newListOfParmaeters);
    }

Second way

This way will be more correct.
You need to create an event to signal the update of data in the ComboBox. In the place that you need, you need to publish an event with a set of parameters for setting.
In the MainView, create a listener for the event that will update the data in the comboBox.

Here’s an article where you can learn more about creating an application events: https://www.baeldung.com/spring-events

Best regards,
Dmitriy

2 Likes