Problem JSpinner getValue()

Hello,

I have a problem with the Jspinner component. I have a jpanel which has a jbutton and a jspinner.

Every time i press my button i do:

private void applyActionPerformed(ActionEvent evt) {

double value=(Double)spinner.getValue();

System.out.println(value);

}

My problem: the jspinner always waits for an enter key press before to take the value that it has in his panel. So when I call my method, it does not get the current value, it gets the last value validated with an "enter"

My question: how can I get the current value in my jspinner without press the enter key

Thanks

[641 byte] By [jatsa] at [2007-11-27 8:01:31]
# 1
Use a ChangeListener.
Hippolytea at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...
# 2
Could you give more details about how would be the correct way to implement a changeListener in my jspinner. I am trying to use it but I cant figure out how it could help me?thanks
jatsa at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...
# 3
Have you worked through the tutorial page on JSpinner? It's covered well there: http://java.sun.com/docs/books/tutorial/uiswing/components/spinner.html
Hippolytea at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...
# 4

I have red the tutorial, I know I can implement a changelsitener to know when the value in the jspinner has changed. But my problem is that the jspinner does not take the value that I enter from the keyboard at less if I press the enter key or if I select it with the arrows from my jspinner.

I need to write a new value in my jspinner from the keyboard, and I need the jspinner gets it without press the enter key or arrows, so when I do a "getValue()" it gets the new value entered. Like a JTextField in which you can write a value and then call the getTextValue() and you get the current value, only I can not use a Jtextfield

jatsa at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...
# 5
Well, if you are typing in a value:1..3..5...Have you entered 135 or are you about to enter 1350? The change listener is only notified when you move focus to another component, for example.Do you want to be notified as each key is pressed?
Hippolytea at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...
# 6
I press my jbutton to know which is the current value entered in my jspinner.I think that what i am trying to do is try to use my jspinner as a jtextfield, but i dont know if it's possible.
jatsa at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...
# 7

Swing related questions should be posted in the Swing forum.

> how can I get the current value in my jspinner without press the enter key

Works fine for me.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...
# 8
you will get the behaviour you describe if what is typed into the spinner is invalid for the range or model of the spinner
Michael_Dunna at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...
# 9

> Do you want to be notified as each key is pressed?

Let me answer that for you... No, you don't. I once used a program that worked that way, and the field in question was "number of degrees to rotate this graphic". So I would type in 2, and it would rotate it 2 degrees, then 7, and it would rotate it 27 degrees, then 0, and it would rotate it 270 degrees. All with the latency involved in rotating a large graphic an arbitrary number of degrees. Horrible. Don't do that.

DrClapa at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...
# 10

try{

spinner.commitEdit();

} catch (ParseException e) {

// the input value was not valid

}

spinner.getValue();

dwga at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...
# 11

Thank for your answer dwg, it works!!!!!

I going to try to explain what was my problem. I have a button and spinner.

When I press my button, I do

spinner.getValue();

If the value of my spinner has been selected with the arrows of the spinner, it works fine and I get the correct value. But...

If I type a value in the text zone of my spinner and then I pressed the button (without use the arrows or press the enter key), the

spinner.getValue() did not return me the value typed in the text zone, it returned me the last value entered with the arrows

I don抰 know if the problem is more clear now, I think most of the problem is because I have not written in English in a long time, sorry about that but thank u very much for the answer!!!

jatsa at 2007-7-12 19:43:37 > top of Java-index,Java Essentials,Java Programming...