Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
Callback Pattern
-
With a callback pattern, you may want to have an action take place once
an event happens.
-
Let's say the object that controls this event takes an input parameter
that is a type of interface.
-
This interface has one method defined. Any class that implements
this interface must implement the method, so we know that any object passed
to the controller will have this method.
-
Then, when the event does take place, you can simply call the method of
that object passed in.
-
This is very common in event handling used in GUIs.
-
The ActionListener interface implements an actionPerformed()
method. This method is called when the event actually happens.
But, only if the object that implements the ActionListener interface is
added to an event controller class first.
-
Many event controller classes have an addActionListener(ActionListener
actionListener); method just for this purpose.
-
We'll cover this in more detail in Swing.
-
Real-life example (if available), or live example with Runnable interface.
-
We'll discuss inner classes, also in Chapter 7, if time permits at the
end of the quarter.
Event Handling
Created by: Brandan
Jones January 4, 2002