Java Adapter classes

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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 class

Listener interface

WindowAdapter

WindowListener

KeyAdapter

KeyListener

MouseAdapter

MouseListener

MouseMotionAdapter

MouseMotionListener

FocusAdapter

FocusListener

ComponentAdapter

ComponentListener

ContainerAdapter

ContainerListener

HierarchyBoundsAdapter

HierarchyBoundsListener

java.awt.dnd Adapter classes

Adapter class

Listener interface

DragSourceAdapter

DragSourceListener

DragTargetAdapter

DragTargetListener

javax.swing.event Adapter classes

Adapter class

Listener interface

MouseInputAdapter

MouseInputListener

InternalFrameAdapter

InternalFrameListener

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. }  

Java MouseAdapter Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class MouseAdapterExample extends MouseAdapter{  
  4.     Frame f;  
  5.     MouseAdapterExample(){  
  6.         f=new Frame("Mouse Adapter");  
  7.         f.addMouseListener(this);  
  8.           
  9.         f.setSize(300,300);  
  10.         f.setLayout(null);  
  11.         f.setVisible(true);  
  12.     }  
  13.     public void mouseClicked(MouseEvent e) {  
  14.         Graphics g=f.getGraphics();  
  15.         g.setColor(Color.BLUE);  
  16.         g.fillOval(e.getX(),e.getY(),30,30);  
  17.     }  
  18.       
  19. public static void main(String[] args) {  
  20.     new MouseAdapterExample();  
  21. }  
  22. }  

Java MouseMotionAdapter Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class MouseMotionAdapterExample extends MouseMotionAdapter{  
  4.     Frame f;  
  5.     MouseMotionAdapterExample(){  
  6.         f=new Frame("Mouse Motion Adapter");  
  7.         f.addMouseMotionListener(this);  
  8.           
  9.         f.setSize(300,300);  
  10.         f.setLayout(null);  
  11.         f.setVisible(true);  
  12.     }  
  13. public void mouseDragged(MouseEvent e) {  
  14.     Graphics g=f.getGraphics();  
  15.     g.setColor(Color.ORANGE);  
  16.     g.fillOval(e.getX(),e.getY(),20,20);  
  17. }  
  18. public static void main(String[] args) {  
  19.     new MouseMotionAdapterExample();  
  20. }  
  21. }  

Java KeyAdapter Example

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class KeyAdapterExample extends KeyAdapter{  
  4.     Label l;  
  5.     TextArea area;  
  6.     Frame f;  
  7.     KeyAdapterExample(){  
  8.         f=new Frame("Key Adapter");  
  9.         l=new Label();  
  10.         l.setBounds(20,50,200,20);  
  11.         area=new TextArea();  
  12.         area.setBounds(20,80,300300);  
  13.         area.addKeyListener(this);  
  14.           
  15.         f.add(l);f.add(area);  
  16.         f.setSize(400,400);  
  17.         f.setLayout(null);  
  18.         f.setVisible(true);  
  19.     }  
  20.     public void keyReleased(KeyEvent e) {  
  21.         String text=area.getText();  
  22.         String words[]=text.split("\s");  
  23.         l.setText("Words: "+words.length+" Characters:"+text.length());  
  24.     }  
  25.   
  26.     public static void main(String[] args) {  
  27.         new KeyAdapterExample();  
  28.     }  
  29. }