Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding and using the Jspinner SpinnerNumberModel value

 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I've got a Jspinner that uses the SpinnerNumberModel and goes from 0 to 30 with an incressement of 1.

I want to be able to have a changeListener method to find and compare the value.

For example, if the user spins the spinner to show the value "3" , then I can do checks on that value to see if it is greater then another value e.g.

So far, I have;



I have a feeling I should be trying to convert the string into an int, but I'm not sure.

So far, I can only think of using the getValue() method, but when I run the code I get nothing. However if I remove the "if (amount1.getValue() == "1") {" line I get some form of output.

Any ideas would be appreciated

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Never ever use == to compare Strings, use .equals

2) The actual value returned by SpinnerNumberModel is not a String but a Number, usually a Double or Integer. In your case it's probably Integer. Compare that:
Note that I used Number instead of Integer. This will allow your code to continue working if you start using Long, Double, Float, Short, etc. All of these allow you to retrieve the value as an int using intValue().
 
Rob Camick
Ranch Hand
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the JSpinner API and follow the link to the section in the Swing tutorial on "How to Use Spinners" where you will find a demo that uses a ChangeListener on a spinner with a DateSpinnerModel. I'm sure your code would be similiar except you are using a number model.
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replys

I'll keep those tips in mind from now on

I managed to fix it and get it working, cheers a lot guys!

(I am trying to learn this, not just use you guys to make the code for me, I do spend an hour or so trying to fiqure out what you've told me to do and what it's doing)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic