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

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

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

Data Warehousing - An Overview

Anurag Information Technology (IT) has historically influenced organizational performance and competitive standing. The increasing processing power and sophistication of analytical tools and techniques have put the strong foundation for the product called data warehouse. There are a number of reasons that any organization should consider a data warehouse, which can be the critical tool for maximizing the organization’s investment in the information it has collected and stored throughout the enterprise. IT managers need to understand the rationale and benefits of data warehouses because they may need to design and implement, or procure this kingpin of business intelligence. The data warehouses are supposed to provide storage, functionality and responsiveness to queries beyond the capabilities of today's transaction-oriented databases. Also data warehouses are set to improve the data access performance of databases. Traditional databases balance the requirement of data access w...