JSpinner Alternative ActionListener

In my program, I want to use JSpinner for a number. This number will later be used to calculate something. Each time the user presses one of the counter buttons (up or down), I want the result to be automatically updated. Since you cannot add an ActionListener to JSpinner (which, in my opinion, is very strange), I ask how to do something similar to this (I already have an ActionListener that can be changed in any other listener of course).

+5
source share
2 answers

You can add ChangeListenerto the counter. This will work when the buttons are pressed (or direct editing of the field).

spinner.addChangeListener(new ChangeListener() {      
  @Override
  public void stateChanged(ChangeEvent e) {
    // handle click
  }
});
+6
source

, ( ), , .

DocumentListener Document , .

Edit:

JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor)number.getEditor();
JTextField textField = editor.getTextField();
textField.getDocument().addDocumentListener( new DocumentListener()
{
    public void insertUpdate(DocumentEvent e)
    {
        System.out.println("insert");
    }

    public void removeUpdate(DocumentEvent e)
    {
        System.out.println("remove");
    }

    public void changedUpdate(DocumentEvent e) {}
});
+1

All Articles