Over the next several lectures we discussed the use of interfaces to decouple components of our software. In particular, we saw an example of how event handling is done in the Java library classes. In particular, we looked at the JButton class and the ActionListener interface:
This class defines a simple pushbutton style component. When a user
clicks their mouse over the button's visual representation, an event
is generated. The event is (informally) that of the button having
been clicked. More technically the event is an object of type
ActionEvent.
The button itself does nothing in response to being clicked. It
is, however, prepared to let someone know that it was clicked. We can
attach an ActionListener to the button; any ActionListeners that the
button has been told about will be notified when the button is
clicked.
This interface specifies one method:
public void actionPerformed(ActionEvent e);
To handle an ActionEvent (i.e. a click on a button) we need to define
a class which implements this interface (i.e. which defines the
required method and which is of the ActionListener type), and then
register an instance of the class as an event handler for the button
(by calling its addActionListener method).
There is example code in the lecture code repository, under the "EventHandling" project.