I've been having some problems using the JSlider class - specifically with tick labels.
The first time I use setMajorTickSpacing
and setMinorTickSpacing
everything works as expected. However, subsequent calls to setMajorTickSpacing
update the ticks, but not the labels. I've written a simple example to demonstrate this behaviour:
import java.awt.event.*;
import javax.swing.*;
public class SliderTest {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setSize(300, 250);
JSlider slider = new JSlider(0, 100, 0);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(1);
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setMajorTickSpacing(25);
slider.setMinorTickSpacing(5);
frame.add(slider);
frame.pack();
frame.setVisible(true);
}
}
Two simple work-arounds seem to fix the problem - either using slider.setLabelTable(null)
or slider.setLabelTable(slider.createStandardLabels(25))
before the second call to setMajorTickSpacing
. Given this, it would seem that the label table is not being updated correctly.
I'm not sure if this is the intended behaviour or not. My first instinct is that updating the tick spacing should also update the labels, but there are also arguments for separating the two.
So I'd like to know which it is - is this a bug in JSlider
or the intended behaviour? If it is the intended behaviour, what would be the standout reasons for making that choice?