Jmix Component access to set required value for dynamic UI component

Hello,

I’ve got a screen where UI components build dynamically based on saved data. I’m able to access these for show/hide functionality in the following way:

    private void hideAllQuestions() {
        for (SurveyQuestion question : surveyQuestions) {
            String questionComponentId = getQuestionComponentId(question);
            Component component = Objects.requireNonNull(questionsBox.getComponent(questionComponentId));
            component.setVisible(false);
        }
    }

(Obviously I could just hide the UI container as a whole, but this was just an example of how I’m doing it.)

This works beautifully. However I’m not able to access the setRequired method. My customer has one particular component that they would like to change to not required based on another component’s selected value, but they want the component to still be visible so the users can provide information either way. How can I achieve this? Is there a way to cast the component directly based on instanceof type to access the setRequired modifier?

Thanks,
Adam

Yep, that was how. The last question I asked got me thinking.

if (component instanceof RadioButtonGroup<?>) {
    RadioButtonGroup<SurveyAnswerOption> option = (RadioButtonGroup<SurveyAnswerOption>) component;
    option.setRequired(false);
}

Good stuff!

1 Like