Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How to Implement Adapter Class in Java

Last updated on Jun 17,2021 6.5K Views


Adapter Class in Java is a very interesting topic that everyone must know about. In this article we will discuss the following topics:

 

Introduction to Adapter Class

Adapter classes provide an implementation of listener interfaces. When you inherit the adapter class implementation for all methods is not mandatory. Thus writing excess code is saved.

These adapter classes can be found in java.awt.event, java.awt.dnd and javax.swing.event packages. Some of the common adapter classes with corresponding listener interfaces are given below.

  • java.awt.event
  • java.awt.dnd
  • javax.swing.event

 

java.awt.event

Adapter ClassListener Interface
WindowAdapterWindowListener
KeyAdapter
KeyListener
MouseAdapter
MouseListener
MouseMotionAdapter
MouseMotionListener
FocusAdapterFocusListener
ComponentAdapter ComponentListener
ContainerAdapterContainerListener
HierarchyBoundsAdapterHierarchyBoundsListener

 

java.awt.dnd

Adapter ClassListener Interface
DragSourceAdapterDragSourceListener
DragTargetAdapterDragTargetListener

 

javax.swing.event

Adapter ClassListener Interface
MouseInputAdapterMouseInputListener
InternalFrameAdapterInternalFrameListener

 

Java Mouse Adapter

import java.awt.*;  
import java.awt.event.*;  
public class MouseAdapterExample extends MouseAdapter{  
    Frame f;  
    MouseAdapterExample(){  
        f=new Frame("Mouse Adapter");  
        f.addMouseListener(this); 
        f.setSize(300,300);  
        f.setLayout(null);  
        f.setVisible(true);  
    }  
    public void mouseClicked(MouseEvent e) {  
        Graphics g=f.getGraphics();  
        g.setColor(Color.BLUE);  
        g.fillOval(e.getX(),e.getY(),30,30);  
    }   
public static void main(String[] args) {  
    new MouseAdapterExample();  
}  
}  

Mouse Adapter Class in Java

 

 

Java MouseMotionAdapter

import java.awt.*;  
import java.awt.event.*;  
public class MouseMotionAdapterExample extends MouseMotionAdapter{  
    Frame f;  
    MouseMotionAdapterExample(){  
        f=new Frame("Mouse Motion Adapter");  
        f.addMouseMotionListener(this);  
        f.setSize(300,300);  
        f.setLayout(null);  
        f.setVisible(true);  
    }  
public void mouseDragged(MouseEvent e) {  
    Graphics g=f.getGraphics();  
    g.setColor(Color.ORANGE);  
    g.fillOval(e.getX(),e.getY(),20,20);  
}  
public static void main(String[] args) {  
    new MouseMotionAdapterExample();  
}  
}  

Mouse Motion Adapter

 

 

Java KeyAdapter Class

import java.awt.*;  
import java.awt.event.*;  
public class KeyAdapterExample extends KeyAdapter{  
    Label l;  
    TextArea area;  
    Frame f;  
    KeyAdapterExample(){  
        f=new Frame("Key Adapter");  
        l=new Label();  
        l.setBounds(20,50,200,20);  
        area=new TextArea();  
        area.setBounds(20,80,300, 300);  
        area.addKeyListener(this);  
          
        f.add(l);f.add(area);  
        f.setSize(400,400);  
        f.setLayout(null);  
        f.setVisible(true);  
    }  
    public void keyReleased(KeyEvent e) {  
        String text=area.getText();  
        String words[]=text.split("s");  
        l.setText("Words: "+words.length+" Characters:"+text.length());  
    }  
  
    public static void main(String[] args) {  
        new KeyAdapterExample();  
    }  
}

Key Adapter

 

 

Advantages of the Adapter Class

It assists unrelated classes to work together and provides a way to use classes in multiple ways. It can be able to increase the transparency of classes. An adapter class provides a way to include related patterns inside a class. Users are provided an option of a pluggable kit for developing applications. Thus, the usage of classes become highly reusable.

 

Adapter Design Pattern

An adapter design pattern is a structural design pattern, which allows two different interfaces to work together. The adapter pattern is capable of making two incompatible interfaces compatible without changing their existing code. The corresponding interfaces may be incompatible, but inner functionalities should match the requirement.

The adapter pattern is frequently made to suit an existing class with others without modifying their source code. In addition, they use a single class to join functionalities of independent or incompatible interfaces. Another name for the adapter pattern is known as the wrapper, i.e it is an alternative name shared with the decorator design pattern.

The pattern also converts the incompatible interfaces of a class into different interfaces that are nothing but the target. This is what the clients require in the end. Adapter patterns also let classes work together else it would be almost incompatible for interfaces to function together. To bring things into perspective, consider a person traveling frequently to different countries with his laptop and mobile.

There are different electric sockets, voltages, and frequency measured in different countries and that makes the use of any appliance of one country to be compatible in a different country. In the UK, a Type G socket with 230 volts and 50 Hz frequency is commonly used.

In the US, a Type A and Type B sockets with 120 volts and 60 Hz frequency are practiced. In India Type C, Type D. and Type M sockets with 230 volts and 50 Hz is used. Lastly, in Japan, Type A and Type B sockets with 110 volts and 50 Hz frequency are used. Thus, it can be concluded that the appliances we carry may be incompatible with the electric specifications we have at different places. Similarly, adapter tools are essential because they can convert incompatible code into compatible code.

With this, we come to the end of this Adapter Class in Java. Check out the Java Online Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course are designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Adapter Class in Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 22nd February,2025

22nd February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to Implement Adapter Class in Java

edureka.co