Anonymous Adapter Class
An Adapter class is provided in java.awt.event package for those Listener interfaces which are having more than one method. Such as MouseAdapter, WindowAdapter, etc. Adapter class is derived from Listener interface and it provides blank definition of all the methods of derived listener. So that when we use Adapter class instead of Listener interface we can skip writing any of the method which are unused.
Characteristics:
- Components/Event Sources can be easily accessed from Listener methods
- To apply an event we have to pass anonymous object of Adapter class
- Logic is to be written in complex inner methods
- Not compulsory to define all the methods. No need to write Blank definition if not required.
Code:
import java.awt.*; import java.applet.*; import java.awt.event.*; public class EventDemo4 extends Applet { Button btn; Label lbl; public void init() { btn = new Button("Click Me!"); lbl = new Label(" "); add(btn); add(lbl); btn.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { lbl.setText("Clicked"); } }); } } /* <applet code="EventDemo4" width="500" height="50"></applet> */
Recent Comments