I have a JSlider object named quantitySl
and a JSpinner object named quantitySp
. When someone changes the spinner I want to change the slider to the same value and vice versa.
The initialization of the objects is as follows:
SpinnerNumberModel quantityLimiter = new SpinnerNumberModel(1, 0, 20, 1);
JSpinner quantitySp = new JSpinner(quantityLimiter);
JSlider quantitySl = new JSlider(0,20,1);
quantitySl.addChangeListener(this);
quantitySp.addChangeListener(this);
Here is the code with which I'm trying to change the values:
@Override
public void stateChanged(ChangeEvent ce) {
if(ce.getSource()==quantitySp){
quantitySp.setValue(quantitySl.getValue());
}
else if(ce.getSource()==quantitySl){
quantitySl.setValue((int) quantitySp.getValue());
}
}
The problem is that changing the value of the spinner triggers its statechanged
event which results in locking of both those input fields. How can I solve this?