Group checkboxes in form by dropdown entries and add as headline

Hello, I have a form in which I use many checkboxes to assign many connected records to the current record. I can add the individual groups of connected records beforehand using a dropdown (also connected records).

If I add another group in the dropdown the resulting list of checkboxes gets longer and longer. I would like to have the “group name” from the drop down menu as a title for the corresponding checkboxes in the list. As a sort of devider or headline, because in the flat list of checkboxes no grouping is shown. I hope the problem is underandable. Anyone having advice?

Thanks.

Markus