0

I want to take null values when my JSpinner is empty. But it returns 0.0 when I call getValue() function.

  public JFormattedTextField getTextField(JSpinner spinner) {
    JComponent editor = spinner.getEditor();
    if (editor instanceof JSpinner.DefaultEditor) {
        return ((JSpinner.DefaultEditor)editor).getTextField();
    } else {
        return null;
    }
}

And this is my method call:

Object a = getTextField(jValueNum).getValue();

Edit: or is there any way to OverLoad the getValue() function?

user2279774
  • 53
  • 1
  • 7

3 Answers3

2

@user2279774: Set jSpinner with user defined values. That will be only way I know.. eg:

String[] colors = new String[] {
                "Red", "Orange", "Yellow", "Green", "Blue","  ", "Purple"
        };
        SpinnerListModel model = new SpinnerListModel(colors);
SenthilPrabhu
  • 661
  • 6
  • 29
1

But it returns 0.0 when i call getValue() function.

quite not possible without dirty hack for JFormattedTextField with Number Formatter, there is default value whatever number from Number instance

I want to take null values when my jSpinner is empty

return null (wrong idea, because number should be used for computations) in the case that JSpinner returns double.valueOf(0.0)

Object a = getTextField(jValueNum).getValue();

JSpinner can do that too, there no reason to use derived editor for return

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Check what spinner model you have set. Probably you have set SpinnerNumberModel. Try with SpinnerDataModel and set all value in string format.

It will return editor(JFormattedTextField) which is configured for object and you will get null as value. :)

  • i need a SpinnerNumberModel. User should enter a number. But user should be able to leave it blank for null value for variables with no value. – user2279774 May 15 '13 at 12:32
  • [see hack](http://stackoverflow.com/a/9366053/714968) for JFormattedTextField, apply this code to JSpinnrs editors component – mKorbel May 15 '13 at 12:50
  • Not an issue..., Apply Documentlistener or InputVerifier with the editor and accept only numeric data from user but internally keep as a string in the spinner model. :) – Himanshu Upadhyay May 15 '13 at 15:26
  • What is `SpinnerDataModel`? – trashgod May 15 '13 at 19:44