Getting Your Programs to Listen

Before you can receive user input in a Java program, you must learn how to teach an object to listen. Responding to user events in a Java program requires the use of one or more EventListener interfaces. Interfaces are special classes that enable a class of objects to inherit behavior it would not be able to use otherwise.

Adding an EventListener interface requires two things right away. First, because the listening classes are part of the java.awt.event group of classes, you must make them available with the following statement:

import java.awt.event.*;

Second, the class must use the implements statement to declare that it supports one or more listening interfaces. The following statement creates a class that ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.