Java AWT Listeners

Java AWT Listeners are a group of interfaces from java.awt.event package. Listeners are capable to handle the events generated by the components like button, choice, frame etc. These listeners are implemented to the class which requires handling of events.

public class ButtonDemo1 extends Frame implements ActionListener

The class ButtonDemo1 implements ActionListener as ButtonDemo1 includes some buttons which require event handling. The button events (known as action events) are handled by ActionListener.

Even though, some listeners handle the events of a few components, generally every component event is handled by a separate listener. For example, the ActionListener handles the events of Button, TextField, List and Menu. But these types are very rare.

Table giving list of few Java AWT Listeners and components whose events the listeners can handle.

Listener Interface Component
WindowListener Frame
ActionListener Button, TextField, List, Menu
ItemListener Checkbox, Choice, List
AdjustmentListener Scrollbar
MouseListener Mouse (stable)
MouseMotionListener Mouse (moving)
KeyListener Keyboard

A program to close the window using WindowListener is available at "Java Closing Frame – WindowListener".

From JDK 1.1, Java AWT Listeners are replaced by Java AWT Adapters. Adapters make event handling code simple to write. But adapter is an abstract class where as listener is an interface. Both are from java.awt.event package.

Any listener interface containing more than one abstract method has got corresponding adapter class. But ActionListener, ItemListener and AdjustmentListener do not have corresponding adapter class as they come with only one abstract method.

Leave a Comment

Your email address will not be published.