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
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
As easy as:
Integer valor = (Integer)miJSpinner.getValue();
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();
}
}