3
$\begingroup$

I want to create two RadioButtonBar objects that offer choices which are dynamically updated by a CheckboxBar. I also need to prevent them from having the same value. I got this far.

My Problem : When the currently selected button of a RadioButtonBar has its value updated through the CheckboxBar, it becomes de-selected. In other words, the value of the RadioButtonBar is updated, but not the value of the variable controlled by it.

I provide a toy example here, but in my real situation, I have many more dynamic lists and buttons. Hence, I'm looking for a solution that doesn't require each dList or dRadio to perform a long list of checks to determine what needs to be updated.

DynamicModule[{dList1 = {a}, dList2 = {d}, dList3 = {g}, dRadio1, dRadio2},
 dRadio1=dList1;
 dRadio2=dList2;

 Grid[{

   {"List 1:", CheckboxBar[Dynamic[dList1], {a, b, c}]},
   {"List 2:", CheckboxBar[Dynamic[dList2], {d, e, f}]},
   {"List 3:", CheckboxBar[Dynamic[dList3], {g, h, i}]},

   {"Radio 1:", Dynamic[RadioButtonBar[
      Dynamic[dRadio1, {(If[# == dRadio2, dRadio2 = dRadio1]) &, Automatic}],
      {dList1, dList2, dList3}]]},
   {"Radio 2:", Dynamic[RadioButtonBar[
      Dynamic[dRadio2, {(If[# == dRadio1, dRadio1 = dRadio2]) &, Automatic}], 
      {dList1, dList2, dList3}]]},
   {Dynamic[dRadio1]},
   {Dynamic[dRadio2]}}]]
$\endgroup$
  • $\begingroup$ Could you provide an output you'd expect with some arbitrary input? $\endgroup$ – e.doroskevic Aug 8 '16 at 13:23
  • $\begingroup$ Sure! if you use the code as provided, the first RadioButtonBar should have the first choice {a} selected. If you change dList1 to be {a,b} through the CheckboxBar, the value of the RadioButtonBar should update (as it does) and the first choice should still be selected. EDITED TO REFLECT CODE CHANGE. $\endgroup$ – Lokdal Aug 8 '16 at 14:00
  • 1
    $\begingroup$ So, the problem is that it simply de-selects the value in your RadioButtonBars ? What about those dRadio1 and dRadio2 do they need to also update? What would they update to? $\endgroup$ – e.doroskevic Aug 8 '16 at 14:03
  • $\begingroup$ They would need to update to the new value of the list they had previously selected. If dRadio1 = dList1 and dList1 changes, dRadio1 should update so that dRadio1 = dList1. $\endgroup$ – Lokdal Aug 8 '16 at 14:04
2
$\begingroup$

You can make your code work in the described way by simply changing the RadioButtonBar input to a {val1 -> labl1, ...} form.

DynamicModule[{dList1 = {a}, dList2 = {d}, dList3 = {g}, dRadio1, dRadio2},
 dRadio1 = 1;
 dRadio2 = 2;
 Grid[{
   {"List 1:", CheckboxBar[Dynamic[dList1], {a, b, c}]},
   {"List 2:", CheckboxBar[Dynamic[dList2], {d, e, f}]},
   {"List 3:", CheckboxBar[Dynamic[dList3], {g, h, i}]}, {"Radio 1:", 
    Dynamic[RadioButtonBar[
      Dynamic[dRadio1, {(If[# == dRadio2, dRadio2 = dRadio1]) &, 
        Automatic}], {1 -> dList1, 2 -> dList2, 
       3 -> dList3}]]}, {"Radio 2:", 
    Dynamic[RadioButtonBar[
      Dynamic[dRadio2, {(If[# == dRadio1, dRadio1 = dRadio2]) &, 
        Automatic}], {1 -> dList1, 2 -> dList2, 3 -> dList3}]]},
   {Dynamic[{dList1, dList2, dList3}[[dRadio1]]]},
   {Dynamic[{dList1, dList2, dList3}[[dRadio2]]]}}]]
$\endgroup$
  • $\begingroup$ That did it, thanks! Are you able to explain why the integer values get updated, but not the variable ones? $\endgroup$ – Lokdal Aug 9 '16 at 2:24
  • $\begingroup$ @Lokdal For example, when dList1 is changed by the CheckboxBar, then dRadio1 stays the same. In my code the value of dRadio1 still matches its previous value and only the label is changed. In your code, as dList1 is changed, the value of dRadio1 no longer matches any of the {dList1, dList2, dList3}. In other word if dRadio1 matched dList1 (first radio button) before changing dList1, it doesn't match it after changing dList1. Its like having RadioButtonBar[0, {1, 2, 3}] now. Changing labels don't matter. $\endgroup$ – Karsten 7. Aug 9 '16 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.