Event Adapters in the AWT Package
The java.awt.event
package provides adapters for various event-listener types. These
adapters are nothing more than empty shells that implement the
methods in the various listener interfaces. These adapters
don’t provide much added value, but you can subclass them to
build basic demultiplexing adapters. They relieve you from having to
implement every interface method yourself, although the burden is
quite small.
Let’s look at an example. The following chunk of code is the
listing for the
java.awt.event.FocusAdapter
class:
public class FocusAdapter implements FocusListener
{
public FocusAdapter()
{
}
public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
}
}So, as you can see, these adapters aren’t all that helpful.
You’ll find an adapter class in
java.awt.event for every event-listener interface
that has more than one method in it. I think the fact that these
adapters are provided at all is an indication that the technique is
expected to be widely used. But I expect that more complex adapters
will find more use than these.
A Generic Button Adapter
It’s common for a dialog box or
other window to contain multiple Button
objects—a perfect place to make use of an intelligent adapter.
The GenericTemperatureAdapter class shown earlier
can be easily modified for this purpose. Let’s create an
adapter that will handle the ActionListener
interface for multiple buttons, and will forward the
ActionEvent to a method on the target object. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access