I need to display an error message and NOT change value of a spinner if user types a value outside of the bounds.
If the spinner buttons are used there is no issue. But if user types a number lower than lower bound, the spinner automatically sets it to the lower bound value. Which may be good BUT I need to make sure user is aware.
SpinnerNumberModel spin = new SpinnerNumberModel(10, 10, 100, 5);
mySpin = new JSpinner();
mySpin.setModel(spin);
If user types in 3 the spinner will get set to 10. But I need to confirm with user that this is what he wants.
EDIT 1
I coded up TIM B's suggestion.
I get the JOptionPane when I change value with spinner buttons. But it does not get triggered if I edit the field manually.
import javax.swing.JOptionPane;
import javax.swing.SpinnerNumberModel;
public class MySpinnerNumberModel extends SpinnerNumberModel{
public MySpinnerNumberModel(int def, int start, int end, int increment){
this.setValue(def);
this.setMinimum(start);
this.setMaximum(end);
this.setStepSize(increment);
}
public void setValue(Object value) {
JOptionPane.showMessageDialog(null,"VALUE: "+value);
super.setValue(value);
if (value instanceof Integer) {
int v = (Integer)value;
//JOptionPane.showMessageDialog(null,"VALUE: "+v);
// Check v here and act as appropriate
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}