0

I have two JSpinner one is for minimum and another one for maximum value set.

I just want that if min is set some value then user can not enter less than minimum text field value in JSpinner of maximum text field and vice versa.

How can I validate using keylistner and another trick?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
manav
  • 119
  • 1
  • 2
  • 9
  • please show a SSCCE that demonstrates the problem (personally, I don't quite understand how/where text fields could possibly come into play anywhere ...;) – kleopatra Nov 27 '12 at 11:36
  • You might want to validate the values later after the user clicks submit button or smth. – Nikolay Kuznetsov Nov 27 '12 at 11:37
  • @kleopatra an editable `JSpinner` has a `JTextField` as editor, which can be replaced by a `JFormattedTextField` for validation. That being said, that is not needed for this question as you can limit the values for your model to an interval – Robin Nov 27 '12 at 13:06
  • @Robin ahh ... that one. Still confused, though: it protects itself (or better the spinner) from out of range values, so still not see the point of this question – kleopatra Nov 27 '12 at 13:29

4 Answers4

2

You can use SpinnerNumberModel where set minimum and maximum values

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • but i have take two spinner and I have to validate according both – manav Nov 27 '12 at 11:29
  • 1
    @manav your question is vague, no additionl infos here, everything including your comment here are shots to the dark, post an SSCCE with detailed descriptions whats who, where, when, how ..... – mKorbel Nov 27 '12 at 11:47
  • @manav see `BoundedRangeModel`, could be help for hightlevel events betweens `view_to_model`, meaning for finalized value stored in model (`JSpinner.NumberEditor`) – mKorbel Nov 27 '12 at 11:54
2

you can set a custom spinnermodel. for numbers try

new JSpinner(new SpinnerNumberModel(initvalue,minvalue,maxvalue));

http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html

schippi
  • 1,034
  • 5
  • 15
2

You need to attach a ChangeListener to each of spinners (model).

When the stateChanged event is fired, you need to determine from which model the event occurred and update the other model accordingly.

So if the event came form min model, you'd need to update the max models min value.

Note, this may cause a cascading series of updates, so you need to be prepared for that

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I want on keypressed event ,if user wants to press invalid value the Jspinner textfield deny to enter it – manav Nov 27 '12 at 11:45
  • No you don't. If you want to prevent the user for typing a invalid value, you should use a [document filter](http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html). Getting it attached is problematic – MadProgrammer Nov 27 '12 at 18:49
0

Just as MadProgrammer suggested, ChangeListener is the right approach.

Here's a simple code sample:

    //class fields
    JSpinner lowSpin = new JSpinner(new SpinnerNumberModel()); 
    JSpinner highSpin = new JSpinner(new SpinnerNumberModel());

    ChangeListener spinListener = new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            int low = (int) lowSpin.getValue();
            int high = (int) highSpin.getValue();
            if(high < low)
                JOptionPane.showMessageDialog(null, "HIGH should not be less than LOW."); //you can change 'null' to the name of your JFrame
        }
    };

    lowSpin.addChangeListener(spinListener);
    highSpin.addChangeListener(spinListener);
davidXYZ
  • 719
  • 1
  • 8
  • 16
  • thanks david, but I want to use keylistner for Jspinner TextField .how to validate Jspinner text field to restrict value less than 16 only – manav Nov 29 '12 at 11:27
  • There's a `JSpinner` constructor that takes a `SpinnerNumberModel` instance. This class in turn has a constructor where you specify minimum and maximum values. Just specify 16 as min value. You dont need a listener to do that. Also, regarding the comparison check, you can still use KeyListener. Just place the content of the `stateChanged` method above inside KeyListener's `keyPressed` method. (Don't forget to addKeyListener on the JSpinner.) http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/SpinnerNumberModel.html – davidXYZ Nov 29 '12 at 14:56