×

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

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Java LinkedHashSet

LinkedHashSet in Java with Example Java LinkedHashSet extends HashSet and Implements the Set interface. It doesn’t contain only duplicate values like HashSet. It also permits the null elements. It maintains the order...

5 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

How to Convert String to double in Java

How to Convert String to double in java It is used if we have to perform mathematical operations on the string that contains a double number. When we get data from...

3 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

How to Create Different Packages for Different Classes in Java

Packages in Java In Java, Packages are an assortment of classes, sub-packages, and connection points. i.e. A package addresses a word reference that contains a connected gathering of styles and points...

7 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

How to Convert int to long in Java

How to Convert int to long in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

Java String contains() method

contains() method returns true only if it contains the given sequence of characters otherwise it returns false. syntax: public boolean contains(CharSequence sequence) parameters: sequence : It is the sequence to be searched. Returns: It returns true...

1 minute read.

Java Instanceof Keyword

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). The type of comparison in java differentiates the...

4 minutes read.