0
$\begingroup$

I have a SetterBar with 3 values. I want that when I hover the cursor on any of the SetterBar value,it changes its color and again go back to its original. But when I select some value out of 3 values. It should show selected values as Red and rest White. I have tries this way but when I hover cursor of any one of these values, all changes color. This I dont want.

 col = Yellow;
 SetterBar[x,{
       inp1 =  EventHandler[InputField[a, FieldSize -> 3,Background -> Dynamic[col]],
        {"MouseMoved" :> (col =col /. {Yellow -> Green, Green->Yellow})}],            
       inp2 = EventHandler[InputField[a, FieldSize -> 3,Background -> Dynamic[col]],
        {"MouseMoved" :> (col =col /. {Yellow -> Green, Green->Yellow})}], 
       inp3 = EventHandler[InputField[a, FieldSize -> 3,Background -> Dynamic[col]],
        {"MouseMoved" :> (col =col /. {Yellow -> Green, Green->Yellow})}]
  }, Enabled -> True]
$\endgroup$

1 Answer 1

2
$\begingroup$

As I understand your question, you simply want a setterbar which automatically selects a value when you hover over it, and secondly you want to have displayed elements depend on the selected value. What you can then do is to simply write the code for you setter bar:

 SetterBar[Dynamic[x], Range[3], AutoAction -> True, Appearance -> None]

And then slightly change the value specification. When you specify in the form {1->myfun[1],2->myfun[2]} the value it uses myfun[1] to display the button, but still selects 1. So then you could add a dynamic panel which changes color to red if it's value is equal to the selected value:

 turnRed[n_] := (n -> Dynamic@Panel[n, Background -> If[x == n, Red, White]])
 SetterBar[Dynamic[x], turnRed /@ Range[3], AutoAction -> True, Appearance -> None]

Using this code, you should note that x is not passed to turnRed[_], so if you use DynamicModule or similar to scope your code, you would need to define turnRed inside this scope.

Note: On my system (Mathematica 8.04 on Windows xp) I get a syntax error highlighting on AutoAction, but it still works as expected.

$\endgroup$
1
  • 1
    $\begingroup$ +1. On my system I get that error too and the colour flickers with AutoAction set to true. Btw, the Dynamic could be attached to Background->Dyynamic@If.... I don't see a noticeable difference but in general it's a good idea to minimize what you update $\endgroup$
    – Rojo
    Aug 16, 2012 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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