Skip to main content

JAVA Adapter Class

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

java.awt.dnd Adapter classes

Adapter classListener interface
DragSourceAdapterDragSourceListener
DragTargetAdapterDragTargetListener

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

Output:

java awt windowadapter example 1

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

Output:

java awt mouseadapter example 1

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

Output:

java awt mousemotionadapter example 1

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

Output:

java awt keyadapter example 1

Anurag Rana Educator CSE/IT

Comments

Post a Comment

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);   ...

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari...

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...