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.
"The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!', but 'That's funny...' "
- Isaac Asimov
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.
The quieter you are, the more you are able to hear.
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.
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?
Joined: Oct 13, 2005
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.
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...
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
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.
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.