How to add values from JSpinners?

0

I want to make a simple program where the user puts a number in JSpinner and, when clicking a button, it will appear in another JSpinner , the value of the first added by 2.
However, I can not make the sum between the value indicated in JSpinner and 2, because the value of JSpinner is not considered int . How do I do?

Here is a sample code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    Object w = jSpinner2.getValue();
    jSpinner1.setValue(w+2); //Aqui acontece o erro  

    //JSpinner1 é o valor final.
    //JSpi nner2 é o valor indicado pelo usuario.
}  
    
asked by anonymous 23.06.2016 / 14:34

1 answer

1

Try to cast to Integer before adding the value in the new Spinner:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    int w = (Integer)jSpinner2.getValue();//cast de object para int
    jSpinner1.setValue(w+2);  

} 
    
23.06.2016 / 14:45