×

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces

Advantages of Adapter classes

  • Adapter class offers a means of incorporating similar patterns into the class.
  • It offers a pluggable development kit for applications.
  • By using the adapter class, it makes the class more versatile.
  • Adapter classes makes  easier for unrelated classes to collaborate.
  • It offers diverse approaches to using classes.
  • It makes lessons more transparent.

To use the adapter class,mustd to import the following packages

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

java.awt.event adapter class

Adapter classListener Interface
Mouse AdapterMouse Listener
Key AdapterKey Listener
Window AdapterWindow Listener
MouseMotionAdapterMouseMotionListener
ContainerAdapterContainerListener
ComponentAdapterComponentListener

java.awt.dnd adapter class

Adapter classListener Interface
DragSourceAdapterDragSource Listener
DragTargetAdapterDragTarget Listener

java.swing.event adapter class

Adapter classListener Interface
Mouse Input AdapterMouse  Input Listener
Internal Frame AdapterInternal Frame Listener

Program for Java Mouse Adapter

MouseAdapter1.java

import java.awt.*;  
import java.awt.event.*;  
public class MouseAdapter1 extends MouseAdapter{  
    Frame f1;  // Creating a frame
    // Default constructor
    MouseAdapter1(){  
        f1=new Frame("Mouse Adapter");  // Setting the name of the frame as MouseAdapter
        f1.addMouseListener(this); 
        f1.setSize(400,400);  // Setting the size of frame 
        f1.setLayout(null);  
        f1.setVisible(true);  // Giving visibility to the frame
     // This inner class is used to close the window
        f1.addWindowListener (new WindowAdapter() 
{    
            public void windowClosing (WindowEvent e1) {    
                f1.dispose();    
            }    
        });   
    }  
    public void mouseClicked(MouseEvent e) {  
        Graphics g1=f1.getGraphics();  
        g1.setColor(Color.BLACK);  
        g1.fillOval (e.getX(), e.getY(), 20, 15);    
    }   
public static void main(String[] args) {  
    new MouseAdapter1();  
}  
}

Output

Adapter class in Java

Java Window Adapter Program

This program is used to create windows using awt packages

Adapter.java

// Necessary  packages are imported
import java.awt.*;    
import java.awt.event.*;    
public class Adapter {  
// Creating a Frame  f1  
    Frame f1;    
// default class constructor  
    Adapter() {    
// creating a frame with the title  
        f1 = new Frame ("Adapter Windows");    
// overriding the window closing() method   
        f1.addWindowListener (new WindowAdapter() {    
            public void windowClosing (WindowEvent e1) {    
                f1.dispose();    
            }    
        });    
         // setting the size of the frame
        f1.setSize (350, 450);    
        // setting the size of the frame
        f1.setLayout (null);    
        f1.setVisible (true);    
    }    
// main method  
public static void main(String[] args) {    
    new Adapter();    
// Default constructor 
// Name of the constructor should be same as class name
}    // main method for the program
}  //Adapter

Output

Adapter class in Java

Java MouseMotionAdapter Program

MouseMotionAdapter1.java

import java.awt.*;    
import java.awt.event.*;     
public class MouseMotionAdapter1 extends MouseMotionAdapter {     
// Creating a frame f1    
Frame f1;    
    MouseMotionAdapter1() {    
        f1 = new Frame ("Adapter Program for mouse motion");    
        f1.addMouseMotionListener (this);    
// Setting the size to 300 X 300 for the frame
        f1.setSize (300, 300);    
        f1.setLayout (null);    
        f1.setVisible (true);       
        f1.addWindowListener (new WindowAdapter() {    
            public void windowClosing (WindowEvent e1) {    
                f1.dispose();    
            }    
        });    
    }    
public void mouseDragged (MouseEvent e1) {    
// Creating object for Graphics class to draw in the frame
Graphics g1 = f1.getGraphics();    
// Setting the color to red
    g1.setColor (Color.RED);    
// Drawing oval shaped to draw the described message
g1.fillOval (e1.getX(), e1.getY(), 10, 10);    
}    
// Main method for the program
public static void main(String[] args) {    
    new MouseMotionAdapter1();    
// Default constructor
//Constructor name  should be same as class name i.e., MouseMotionAdapter1 
}    
}

Output

Adapter class in Java

Java KeyAdapter Program

KeyAdapter1.java

//Importing the required packages
import java.awt.*;    
import java.awt.event.*;    
public class KeyAdapter1 extends KeyAdapter {    
// Creating label and textarea  component variables
    Label l1;    
    TextArea area1;    
    Frame f1;    
// class constructor  
    KeyAdapter1() {    
// creating the Frame with the title  
        f1 = new Frame ("Adapter for Key ");    
// creating the Label  
        l1 = new Label();    
// setting the location of the label   
        l1.setBounds (20, 50, 200, 20);    
// creating the text area  
        area1 = new TextArea();  
        area1.setBounds (20, 80, 300, 300);    
        area1.addKeyListener(this);    
// Adding the label and text area to the frame by using the add  method
        f1.add(l1);  
       f1.add(area1);    
// setting the size  
        f1.setSize (400, 400);  
//layout of the frame  
        f1.setLayout (null);
//visibility of the frame    
        f1.setVisible (true);
        f1.addWindowListener (new WindowAdapter() {    
            public void windowClosing (WindowEvent e) {    
                f1.dispose();    
            }    
        });    
    }    
    public void keyReleased (KeyEvent e) {    
        String text1 = area1.getText();    
// splitting the given String
        String words1[] = text1.split ("\\s");    
        l1.setText ("Words: " + words1.length + " Characters:" + text1.length());    
    }    
  // main method for the program 
    public static void main(String[] args) {    
        new KeyAdapter1();    
    }    
}

Output

Adapter class in Java

Related Topics

Bully Algorithm code in Java

Election algorithms include the bully algorithm, mainly used to select a coordinate. To find a coordinator in a distributed system that can carry out the tasks required by other processes,...

4 minutes read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Java instance variable

An instance in object-oriented programming (OOP) is a particular implementation of any object. Each realized version of an item, which might vary in several ways, is an instance. Instantiation is...

3 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

Differences between Lock and Monitor in Java Concurrency

In this tutorial, we will discuss the overview of Lock and Monitor and the differences between them. Introduction Java Concurrency is the ability to perform specific tasks at a time parallelly. The...

4 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

Session Tracking in Java

When a series of requests from the same User (i.e., requests coming from the same browser) occurs over an extended period of time, servlets employ a mechanism known as session...

3 minutes read.

Stone Game in Java

In this tutorial, we will learn to design a stone game in Java. First of all, we will understand what is this game all about. We will grasp it through...

9 minutes read.

Java Numbers

We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc....

2 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.