0

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    
    }    
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
ManInMoon
  • 6,795
  • 15
  • 70
  • 133

1 Answers1

2

You can provide your own implementation of the class enforcing the bounds and show your popup from that. I can't think of any way to do it off hand from the standard classes but implementing your own model is pretty trivial.

JSpinner:

http://docs.oracle.com/javase/7/docs/api/javax/swing/JSpinner.html

Underneath it has a SpinnerModel:

http://docs.oracle.com/javase/7/docs/api/javax/swing/SpinnerModel.html

You are probably using a SpinnerNumberModel:

http://docs.oracle.com/javase/7/docs/api/javax/swing/SpinnerNumberModel.html

Rather than using the default one create your own sub class and over-ride the setValue method:

http://docs.oracle.com/javase/7/docs/api/javax/swing/SpinnerNumberModel.html#setValue%28java.lang.Object%29

To call super.setValue() and then if the value was too low display the warning message.

class MySpinnerNumberModel extends SpinnerNumberModel {
   // You will need to implement the constructors too

   public void setValue(Object value) {
      super.setValue(value);
      if (value instanceof Integer) {
         int v = (Integer)value;
         // Check v here and act as appropriate
      }
   }
}
Tim B
  • 40,716
  • 16
  • 83
  • 128
  • I've added more explanation. – Tim B Jan 02 '14 at 16:48
  • Wow - thank you. That seems very complicated. Is there a full example anywhere? – ManInMoon Jan 02 '14 at 17:23
  • OK I coded that up. But it only sets v when I change spinner. If I attempt to type in a 'too low' value, the spinner reverts to previous value without calling setValue... So I can't catch this type event problem – ManInMoon Jan 02 '14 at 23:32
  • Try using Number instead of Integer. Other than that I'd expect this to work although it's a while since I've done something like that. See if you can get anything useful from: http://developer.classpath.org/doc/javax/swing/SpinnerNumberModel-source.html – Tim B Jan 02 '14 at 23:44