Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]
share|improve this question

1 Answer 1

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.

share|improve this answer
1  
+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 –  Rojo Aug 16 '12 at 20:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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