0

I have change listener, which prints value only if command1RepeatTime JSpinner loss focus. I want to print command1RepeatTime int value after changing value by pressing arrows or typing value in JSpinner (don't need to loss focus).

code:

 command1RepeatTime.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JSpinner spinner = (JSpinner) e.getSource();
                SpinnerModel spinnerModel = spinner.getModel();
                System.out.println(spinnerModel.getValue());
            }
        });
Pieter
  • 1
  • 1
  • 1
    There seem to be a few suggestions at https://stackoverflow.com/questions/3949382/jspinner-value-change-events/7587253 Have you tried them? – Wim Deblauwe May 07 '21 at 11:05

1 Answers1

0

I found answer:

        JComponent comp = spinner.getEditor();
        JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
        DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
        formatter.setCommitsOnValidEdit(true);
        spinner.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                System.out.println("value changed: " + spinner.getValue());
            }
        });

The answer is to configure the formatter used in the JFormattedTextField which is a child of the spinner's editor: formatter.setCommitsOnValidEdit(true);

Pieter
  • 1
  • 1