I am facing the same issue and wonder why setValue is not working on the code i was working on. Here is a short example that works.
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* Write a description of class SSCCESlider here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SSCCESlider {
public static void main(String[] args) {
JFrame frame = new JFrame("Example");
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
final JLabel label = new JLabel();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
frame.add(label, c);
JSlider boxSizeSlider = new JSlider(JSlider.HORIZONTAL, 10, 45, 40);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
boxSizeSlider.setMajorTickSpacing(10);
boxSizeSlider.setPaintTicks(true);
boxSizeSlider.setPaintLabels(true);
boxSizeSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
final JSlider source = (JSlider) e.getSource();
if (!source.getValueIsAdjusting()) {
label.setText(source.getValue() + "");
if (source.getValue() < 30) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
source.setValue(30);
}
});
// wouldnt work
// source.setValue(30);
// wouldnt work
// try {
// SwingUtilities.invokeAndWait(new Runnable() {
// public void run() {
// source.setValue(30);
// }
// });
// } catch (InterruptedException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// } catch (InvocationTargetException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
}
}
}
});
boxSizeSlider.getModel().setValue(30);
frame.add(boxSizeSlider, c);
label.setText(boxSizeSlider.getValue() + "");
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}