Problem: our application behaves currently quite silly and allows users to close a form window after certain changes without any confirmation, instead of pressing the Save button. This makes them complain they have to start afresh.
We are tasked with adding a confirmation dialog: "You have unsaved changes. SAVE / DISCARD / CANCEL". The dialog should not be displayed if none of the data was changed in the GUI, e.g. no text field's content was edited.
The form has scores of JTextFields, JCheckBoxes, JComboBoxes, etc. We could manually add listeners to all of them, setting some isChanged flag to true if any data was changed and then check that flag before optionally displaying the confirmation dialog. However, it's time-consuming and verbose. If some component is added in the future, it's easy to imagine its changes won't be tracked since one would forget to explicitly attach a listener to it.
What is the most optimal solution to this problem?