Use Of Adapter class in Java
An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of the various methods used by Graphical User Interface (GUI) programming languages like Java to handle events.
The user interacts with the system via connected images and graphics in event-driven GUI programming. This means that any user-initiated action, such as moving the mouse to alter the coordinates of the mouse pointer on the screen, clicking a button, or scrolling a webpage, is regarded as a separate event.
Listener interfaces are implemented by default using Java adapter classes. You won't be required to implement every method of listener interfaces if you inherit from the adapter class. Code is thus saved.
The java.awt.event, java.awt.dnd, and javax.swing.event packages contain the adapter classes.
Benefits of utilising adapter classes
- Include helping unrelated classes function together.
- It offers diverse approaches to using classes.
- It makes lessons more transparent.
- It offers a means of incorporating similar patterns into the class.
- It offers a pluggable development kit for applications.
- It makes the class more versatile.
Adapter Class | Listener Interface |
Mouse Adapter | Mouse Listener |
Window Adapter | Window Listener |
KeyAdapter | KeyListener |
Mouse Motion Adapter | MouseMotion Listener |
Container Adapter | Container Listener |
Componenet Adapter | Component Listener |
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( " M ouse 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
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
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