I have a JSlider, which look slik eit initialises with the correct values (i.e the GUI looks correct) however it is apparent from the behaviour of the program that the JSlider is misbehaving.
When the program starts up the slider is in the correct position for the initial value (In my case 40) but the program behaves as if its set to minimum value (in this case 10). only when the slider is moved does the value appear to be changed.
Heres my code:
JSlider boxSizeSlider = new JSlider(JSlider.HORIZONTAL, 10, 45, 40); //initialises to min:10 max: 45, initial val: 40
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 6;
c.gridwidth = 1;
boxSizeSlider.setMajorTickSpacing(10);
boxSizeSlider.setPaintTicks(true);
boxSizeSlider.setPaintLabels(true);
boxSizeSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
if (!source.getValueIsAdjusting()){
boxSize = source.getValue();
}
}
});
boxSizeSlider.setValue(40); //these two lines are two attemts by me to manually set the value
boxSizeSlider.getModel().setValue(40); //to the desired value, neither seem to work
mainPanel.add(boxSizeSlider, c);
Thanks
James