how can I save the value of a JSpinner in a variable?

1

I need to save the value of a JSpinner that the user has chosen in a variable to be able to use it or be able to display it.

the JSpinner goes from 1 to 10 and if the user chooses the number 5 I want to save that number to be able to use it

    
asked by RkuDkd Fix 22.05.2018 в 07:19
source

2 answers

0

As easy as:

Integer valor = (Integer)miJSpinner.getValue();
    
answered by 22.05.2018 в 08:29
0

I'll give you a simple example of how you could use it ..

public class SpinnerTest {
    JTextField campoTexto;
    JSpinner spinner;
    JFrame marco;

    SpinnerTest(){
        campoTexto = new JTextField(20); 
        //Creamos modelo para JSpinner y lo inicializamos
        SpinnerModel modeloSpinner = new SpinnerNumberModel(3, 0, 10, 1);
        spinner = new JSpinner(modeloSpinner);
        //capturamos cambios del JSpinner
        spinner.addChangeListener(e -> {
            campoTexto.setText(spinner.getValue().toString());
            //Guardamos valor del spinner escogido por el usuario
            int valorSpinner = Integer.parseInt(spinner.getValue().toString());
            if(valorSpinner == 5)
                System.out.println("Este es el valor 5");
        });

        //Creacion Marco con los componentes
        marco = new JFrame("JSpinner");
        marco.setLayout(new FlowLayout());
        marco.add(spinner);
        marco.add(campoTexto);
        marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        marco.setVisible(true);
        marco.pack();
    }

    public static void main(String[] args) {
        new SpinnerTest();
    }
}
    
answered by 22.05.2018 в 09:52