Java Adapter Classes


Java adapter classes provide the default implementation of listener interfaces. If you inherit the adapter class, you will not be forced to provide the implementation of all the methods of listener interfaces. So it saves code.

The adapter classes are found in java.awt.event, java.awt.dnd and javax.swing.event packages. The Adapter classes with their corresponding listener interfaces are given below.

java.awt.event Adapter classes

Adapter classListener interface
WindowAdapterWindowListener
KeyAdapterKeyListener
MouseAdapterMouseListener
MouseMotionAdapterMouseMotionListener
FocusAdapterFocusListener
ComponentAdapterComponentListener
ContainerAdapterContainerListener
HierarchyBoundsAdapterHierarchyBoundsListener

javax.swing.event Adapter classes

Adapter classListener interface
MouseInputAdapterMouseInputListener
InternalFrameAdapterInternalFrameListener

Java WindowAdapter Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class AdapterExample{  
  4.     Frame f;  
  5.     AdapterExample(){  
  6.         f=new Frame("Window Adapter");  
  7.         f.addWindowListener(new WindowAdapter(){  
  8.             public void windowClosing(WindowEvent e) {  
  9.                 f.dispose();  
  10.             }  
  11.         });  
  12.           
  13.         f.setSize(400,400);  
  14.         f.setLayout(null);  
  15.         f.setVisible(true);  
  16.     }  
  17. public static void main(String[] args) {  
  18.     new AdapterExample();  
  19. }  
  20. }  





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition