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);
64 setMaximum(sliderMax + extent);
65 setSliderValue(value);
69 * Answers the value of the slider position (descaled to 'true' value)
73 public float getSliderValue()
76 * convert slider max to 'true max' in case of rounding errors
78 int value = getValue();
79 return value == getMaximum() ? trueMax
80 : value / sliderScaleFactor + trueMin;
84 * Sets the slider value (scaled from the true value to the slider range)
88 public void setSliderValue(float value)
90 setValue(Math.round((value - trueMin) * sliderScaleFactor));
94 * Answers the value of the slider position as a percentage between minimum and
95 * maximum of its range
99 public float getSliderPercentageValue()
101 return (getValue() - getMinimum()) * 100f
102 / (getMaximum() - getMinimum());
106 * Sets the slider position for a given percentage value of its min-max range
110 public void setSliderPercentageValue(float pct)
112 float pc = pct / 100f * getMaximum();