In this article we will learn about adapter classes which are used to simplify the event handling process. Sample Java programs are also provided.
Adapter classes are a set of classes which can be used to simplify the event handling process. As we can see in the mouse event handling program here, even though we want to handle only one or two mouse events, we have to define all other remaining methods available in the listener interface(s).
To remove the aforementioned disadvantage, we can use adapter classes and define only the required event handling method(s). Some of the adapter classes available in the java.awt.event package are listed below:
So, the mouse event handling program can be simplified using adapter classes as follows:
public class MyApplet extends JApplet
public void init()
label = new JLabel();
class MyAdapter extends MouseAdapter
this.label = label;
public void mouseClicked(MouseEvent me)
label.setText("Mouse is clicked");