aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to use a JSpinner to gather input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to use a JSpinner to gather input" Watch "How to use a JSpinner to gather input" New topic
Author

How to use a JSpinner to gather input

Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Jun 04, 2012
Posts: 71

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

Joined: Oct 13, 2005
Posts: 39885
    
  28
You will have to work out for yourself how the resource.ButtonFactory methods are written. Alternatively
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

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

Joined: Aug 29, 2012
Posts: 4
I hope this would help you



all I care is learn something from everyone...
"If there is a day, I could live, Single breath,I could take I take all the things into ma head "--edited Laid to Rest-Lamb of God...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

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

Joined: Oct 13, 2005
Posts: 39885
    
  28
Apologies accepted.
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

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

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Mar 17, 2011
Posts: 8250
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Alix Ollivier
Ranch Hand

Joined: Jun 04, 2012
Posts: 71

@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

Joined: Jun 19, 2010
Posts: 825
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use a JSpinner to gather input