I am adapting code from here:
Value Change Listener to JTextField
EDIT 2
The following code gives me an infinite loop of dialogs when I press the up spin
Custom DocumentListeners and formattedTextField don't play nicely with each other, better don't mix. Instead, use a PropertyChangeListener on the text field that listens for changes of its editValid property: whenever that changes to false, you could notify the users
field.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
LOG.info("" + evt);
if ("editValid".equals(evt.getPropertyName())
&& Boolean.FALSE.equals(evt.getNewValue())) {
SpinnerNumberModel model = (SpinnerNumberModel) spin2.getModel();
JOptionPane.showMessageDialog(null,
"Error: Number must be in range [" + model.getMinimum() + " ..." + model.getMaximum() + "]",
"Error Massage",
JOptionPane.ERROR_MESSAGE);
}
}
});
BTW, personally, I agree with Mad - such an intrusive notification tends to annoy me and maybe your users as well ..