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 * the number of nominal positions the slider represents
20 * (higher number = more fine-grained positioning)
22 private static final int SCALE_TICKS = 1000;
25 * 'true' value corresponding to zero on the slider
27 private float trueMin;
30 * 'true' value corresponding to slider maximum
32 private float trueMax;
35 * scaleFactor applied to true value range to give a
36 * slider range of 0 - 100
38 private float sliderScaleFactor;
41 * Constructor that rescales min - max to 0 - 100 for the slider
47 public Slider(float min, float max, float value)
50 setSliderModel(min, max, value);
54 * Sets the min-max range and current value of the slider, with rescaling from
55 * true values to slider range as required
61 public void setSliderModel(float min, float max, float value)
66 sliderScaleFactor = SCALE_TICKS / (max - min);
67 int sliderMax = (int) ((max - min) * sliderScaleFactor);
68 setMaximum(sliderMax);
69 setSliderValue(value);
73 * Answers the value of the slider position (descaled to 'true' value)
77 public float getSliderValue()
80 * convert slider max to 'true max' in case of rounding errors
82 int value = getValue();
83 return value == getMaximum() ? trueMax
84 : value / sliderScaleFactor + trueMin;
88 * Sets the slider value (scaled from the true value to the slider range)
92 public void setSliderValue(float value)
94 setValue(Math.round((value - trueMin) * sliderScaleFactor));
98 * Answers the value of the slider position as a percentage between minimum and
99 * maximum of its range
103 public float getSliderPercentageValue()
105 return (getValue() - getMinimum()) * 100f
106 / (getMaximum() - getMinimum());
110 * Sets the slider position for a given percentage value of its min-max range
114 public void setSliderPercentageValue(float pct)
116 float pc = pct / 100f * getMaximum();