How can I set the values of a JSlider
? I want to assign it specific values, with intervals that are not constant.
Please help
You can set the min and max values of the slider with the constructor:
JSlider mySlider = new Slider(10, 30); // min value of slider, maxValue of slider
As far as I know the range of the slider is uniform and can't be changed. (I am not certain of this though.) What you could do is use the uniform value and map it to the intervals you want. For example, lets' say you want the slider to go from 10 to 10000, but at a logarithmic scale.
Set the min value to 1, (log base 10 of 10 = 1), the max value to 4 (log base 10 of 10,000) = 4. Get the current value of the slider using the getValue()
method and raise 10 to that power with Math.pow()
.
Or you could store the values corresponding to the slider positions with the values you want in array if they cannot be computed.
You can use the setLabelTable(Dictionary labels)
to set custom labels. This page has information on how to create custom labels. As pointed out here you would actually use a HashTable
a class that implements theDictionary
interface.
Cheers!