• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use a JSpinner to gather input

 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to know how to gather input from a JSpinner. I know that for JTextFields, all I need to do is use variableNameHere.getText(), but the closest thing I could find to that in the JSpinner API was getValue(). Because I want to accept the input as an int, and getValue() returns an Object, I would like to know what command is needed to gather the input as an int. Thanks in advance to all who respond.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall send this thread to our GUIs forum
I shall send you to the Java Tutorials, where you will probably find everything you need
 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I please just have an answer? I have been searching for a few hours, and the tutorials have far too much information for my tired brain to look for right now.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to work out for yourself how the resource.ButtonFactory methods are written. Alternatively
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you want to accept integer value from spinner, you should put SpinnerNumberModel to your JSpinner component (you can specify initial, min/max and step values). When you want to retreive a value from spinner, you can use:

Btw, no need to rush anyone here, I'm sure there are other people who are also tired as you are, but still willing to help others - when they find time to do it.
 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry. What I meant was that I often get a response of "Look in the API", which doesn't help me at all, seeing as I was just there. I didn't mean to be rude.
 
Vibhatha Lakmal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this would help you

 
Paul Clapham
Sheriff
Pie
Posts: 20735
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alix Ollivier wrote:I'm sorry. What I meant was that I often get a response of "Look in the API", which doesn't help me at all, seeing as I was just there. I didn't mean to be rude.


But in this case you got a response of "Look at the tutorial", which is usually much more practical in the case where you know nothing about a non-trivial set of classes. The tutorial does show you how to use a SpinnerNumberModel, and if you had looked there you would have found that in much less than several hours.

So yes, the API typically isn't helpful for beginners. Always look for the tutorial.
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies accepted.
 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I used Kemal's advice and implemented a model, then used getModel().getValue(). I then made a loop which gathers input every second. I realize this may not be the best solution, but I want it to automatically switch. Anyways, here is my code. The problem is that whenever I type a value into one of the JSpinners, all of them get the same value. I think this is because I have the same model for all of them. Am I correct?
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your spinners all have the same model, you would expect them to display the same values. There appears to be no straightforward way to copy those models. Obviously whoever wroite them thought there would be no need to reuse the same model.
But how are you changing the spinners? Are you displaying them and using your mouse?
Why are you using a loop? That sounds wasteful of resources, because it runs continuously. Get the models and spinners working without loops, then later on workout how to add a Listener. It might be a ChangeListener, but I am not certain.
 
Winston Gutkowski
Bartender
Pie
Posts: 10091
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alix Ollivier wrote:What I meant was that I often get a response of "Look in the API", which doesn't help me at all...

Then what you should do is explain the part of the API that you don't understand. In general they're great, but it could just be a badly explained section, and highlighting it might help to get it altered.

On the other hand, it could be that you have some other misconceptions that are in need of correction...

Winston
 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@CampBell Ritchie:

The reason that I am using a loop is that I want the result to update automatically, without having to click on a button or anything. The reason I am not using a listener is that I had a bit of a scuffle with the API a week or so ago, and I don't want to go back to Listeners any time soon.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got your answer about using the same model for multiple JSpinner components.
However, I would definitely remove that while loop you're using, and simply add ChangeListener to JSpinner component. That way, you'll get information on each value change.

On the other hand, if you really mean this:
The reason I am not using a listener is that I had a bit of a scuffle with the API a week or so ago, and I don't want to go back to Listeners any time soon.

than you should probably avoid Swing until you are back in the game with listeners.
Your way of collecting information from components with while loop is the wrong way.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic