Detect all changes (made by the user) in the text in the form controls

I have a program with the Save () method that saves all data in spinners and text fields in a form. Is there a way to call “Save ()” every time a user makes changes to a text box or spinner? Something like a common event handler.

thank

+5
source share
2 answers

You can connect one instance of ChangeListener to all of your controls that might look (when the inner class):

private class ListenerImpl implements ChangeListener {
   public void stateChanged(ChangeEvent e) { save(); }
}

, Container :

final ListenerImpl l = new ListenerImpl();
for (Component c : getComponents()) {
   if (c instanceof JSpinner) {
      ((JSpinner)c).addChangeListener(l);
   } else if (c instanceof JTextPane ) { } // ... other types of components
}

, , addChangeListener(..) ( ).

+3

, ChangeListener FocusListener Save() . ( , , , FocusListener Save() focusLost.)

+2