How to add Jspinners values?

Asked

Viewed 124 times

0

I want to make a simple program where the user puts a number on a JSpinner and, at the click of a button, will appear in another JSpinner, the value of the first summed by 2.
However, I cannot sum the value indicated in JSpinner and 2, because the value of JSpinner is not considered int. How do I do?

Here’s a sample of the 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.
}  
  • Have you tried parsing for int?

  • Sorry I’m new in java, could you explain to me what is a parse int?

1 answer

1


Try to cast the cast for Integer before adding the value to 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);  

} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.