The purpose of this example is to show you how to create your own EventListener interfaces and to help you understand why it carries some real benefits.

The setup

Suppose we want to create this GUI:

... where changing "mass" or "acceleration" (either by hitting enter or clicking the change button) causes "force" to be recalculated, but changing "force" causes "acceleration" to be recalculated. I'm sure you can imagine many ways to do this. What I want to show you, is how we can do this really nicely if we follow the approach of viewing each label/textfield/button combination as a new GUI widget — one that acts like the builtin widgets. Specifically, we will call this widget a Field and it will have its own kind of event listener FieldChangeListener, and the outside world call register to be listeners for those events. What I want you to do is:
  1. read the code closely and understand how it works, and
  2. think about how this embodies the ideas of encapsulation, data hiding, and separation of interface from implementation, as well as avoiding duplication of code, and
  3. notice how, in the process, we've created this Field class that we can imagine reusing in other projects!

The code