1

I have a tough question which probably has an easy answer (I hope).

I'm using JGoodies Binding to bind a slider to a ValueModel; works great.

Suppose I have the following (contrived) situation, where I want to model buying chocolates and caramels and I have $1.00 with the following UI indicators

  • C1 = "caramel" slider = budget for caramel candies = 0 to 50 cents
  • C2 = "chocolate" slider = budget for chocolate candies = 0 to 50 cents
  • C3 = "change" slider = remainder left over

I want to force a constraint C1+C2+C3 = 100, so that:

  • if I adjust C1 up/down by Δx, C3 adjusts by -Δx
  • if I adjust C2 up/down by Δx, C3 adjusts by -Δx
  • if I adjust C3 up/down by Δx, C1 adjusts by y=-floor(Δx/2), and C2 adjusts by -x-y

(so that the sum of adjustments is exactly 0 in all cases)

The obvious approach is to add a listener to each ValueModel to take the given actions in each case. But then I have to worry about two things:

  • circularity (changes in C1 trigger changes in C3 which then changes C1)
  • synchronization: I want to have a means of accessing the complete set (C1, C2, C3) such that the sum of the three numbers add up to 100.

How can I do this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • are you able implements BoundedRangeModel for derivate of JSlider – mKorbel Jan 05 '12 at 21:33
  • yes, I use JGoodies BoundedRangeAdapter on the ValueModel – Jason S Jan 05 '12 at 21:37
  • 2
    I think that by Hovercraft Full Of Eels and camickr posted here http://stackoverflow.com/questions/7617605/java-jslider-precision-problems very good code for your inspirations – mKorbel Jan 05 '12 at 21:46

0 Answers0