0

I need to put a JSpinner in a JOptionPane. Here is what I've tried:

public void actionPerformed(ActionEvent event) {

    SpinnerNumberModel Model = new SpinnerNumberModel(0.05, 0.00, 1.00, 0.01);
    JSpinner spinner1 = new JSpinner(Model);
    int option2 = JOptionPane.showOptionDialog(null, spinner1, "Input seuille", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
    if (option2 == JOptionPane.CANCEL_OPTION) {
        // user hit cancel                          
        m = 0.05;
    } else if (option2 == JOptionPane.OK_OPTION) {
        // user entered a number
        m = Double.parseDouble(spinner1.getValue().toString());
    }
}

I need to get value fromjSpinner but it s not working

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Stella
  • 21
  • 4

1 Answers1

1

This...

SpinnerNumberModel Model = new SpinnerNumberModel(0.05, 0.00, 1.00, 0.01);
JSpinner spinner1 = new JSpinner(Model);
int option2 = JOptionPane.showOptionDialog(null, spinner1, "Input seuille", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
double m = 0;
if (option2 == JOptionPane.CANCEL_OPTION) {
    // user hit cancel                          
    m = 0.05;
} else if (option2 == JOptionPane.OK_OPTION) {
    // user entered a number
    m = (Double)spinner1.getValue();
}
System.out.println(m);

seems to work fine for me. The JSpinner's value is based on the SpinnerModel. Since the model is processing double values, you should be able to cast it to a double directly

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366