3 import javax.swing.JSlider;
6 * A modified {@code javax.swing.JSlider} that
8 * <li>supports float valued numbers (by scaling up integer values)</li>
9 * <li>rescales 'true' value range to avoid negative values, as these are not
10 * rendered correctly by some look and feel libraries</li>
15 @SuppressWarnings("serial")
16 public class Slider extends JSlider
19 * 'true' value corresponding to zero on the slider
21 private float trueMin;
24 * 'true' value corresponding to slider maximum
26 private float trueMax;
29 * scaleFactor applied to true value range to give a
30 * slider range of 0 - 100
32 private float sliderScaleFactor;
35 * Constructor that rescales min - max to 0 - 100 for the slider
41 public Slider(float min, float max, float value)
44 setSliderModel(min, max, value);
48 * Sets the min-max range and current value of the slider, with rescaling from
49 * true values to slider range as required
55 public void setSliderModel(float min, float max, float value)
60 sliderScaleFactor = 100f / (max - min);
61 int sliderMax = (int) ((max - min) * sliderScaleFactor);
62 setMaximum(sliderMax);
63 setSliderValue(value);
67 * Answers the value of the slider position (descaled to 'true' value)
71 public float getSliderValue()
74 * convert slider max to 'true max' in case of rounding errors
76 int value = getValue();
77 return value == getMaximum() ? trueMax
78 : value / sliderScaleFactor + trueMin;
82 * Sets the slider value (scaled from the true value to the slider range)
86 public void setSliderValue(float value)
88 setValue(Math.round((value - trueMin) * sliderScaleFactor));
92 * Answers the value of the slider position as a percentage between minimum and
93 * maximum of its range
97 public float getSliderPercentageValue()
99 return (getValue() - getMinimum()) * 100f
100 / (getMaximum() - getMinimum());
104 * Sets the slider position for a given percentage value of its min-max range
108 public void setSliderPercentageValue(float pct)
110 float pc = pct / 100f * getMaximum();