Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Java is a well-known general-purpose, object-oriented programming language based on classes with minimal implementation requirements. It is a computer platform for application development. As a result, Java is a fast, secure, and dependable programming language. It is commonly used to build Java applications in laptops, data centers, game consoles, scientific supercomputers, cell phones, and other locations.
The Java adapter class is the default implementation of listener interfaces in Java offered via adapter classes. We do not have to implement all listener interface functions if we inherit the Java adapter class. As a result, much code is saved.
The following article will look into Java adapter classes in detail.
A Java adapter class allows listener interfaces to be implemented by default. The Delegation Event Model gave rise to the concept of listener interfaces. It is one of the various strategies used to handle events in Graphical User Interface (GUI) programming languages like Java.
In most event-driven GUI development, the user interacts with the system through related images and graphics. This means any action done by the user is regarded as a separate event, such as moving the mouse pointer on the screen to change its coordinates, clicking a button, or scrolling a page.
These different event activities are inextricably linked to a code segment that iterates the application's response to the user. The path is pretty straightforward. The user generates an event that is sent to one or more listener interfaces. When an event potential is received, the Listener Interface analyses it and responds appropriately.
This path validates the event handling process. A Java adapter class implements an interface with a set of dummy methods. When programmers inherit a Java adapter class, they are not obligated to implement all of the methods mentioned under each listener interface. The adapter class can also be subclassed, allowing the programmer to override only the required methods. In other words, a programmer can quickly create their listener interface to field events by utilizing a Java adapter class—this aids in the reduction of code.
Relationship Between Java Adapter Class and Listener Interface
Listeners are used when a programmer intends to use the majority of the methods given in the interface. If a class implements a listener interface directly, it must implement all of the interface's functions, resulting in overly long code. The use of a Java adapter class can aid in the resolution of this issue. A Java adapter class comes in helpful when an event requires a few specialized methods.
To use a Java adapter class, the programmer only needs to create a subclass of it and override the interest methods. Java Adapter classes are advantageous for Java listener interfaces with several methods.
Types of Java Adapter Classes
Java adapter classes can be found in java.awt.event, java.awt.dnd, and javax.swing.event packages. The adapter classes are listed below, along with their listener interfaces.
java.awt.event Adapter classes
Java adapter class
Java Listener interface
WindowAdapter
WindowListener
KeyAdapter
KeyListener
MouseAdapter
MouseListener
MouseMotionAdapter
MouseMotionListener
FocusAdapter
FocusListener
ComponentAdapter
ComponentListener
ContainerAdapter
ContainerListener
HierarchyBoundsAdapter
HierarchyBoundsListener
java.awt.dnd Adapter classes
Java adapter class
Java Listener interface
DragSourceAdapter
DragSourceListener
DragTargetAdapter
DragTargetListener
javax.swing.event Adapter classes
Java Adapter class
Java Listener interface
MouseInputAdapter
MouseInputListener
InternalFrameAdapter
InternalFrameListener
Example of Java Adapter Classes
Java WindowAdapter
The WindowAdapter class of AWT is implemented in the following example, and one of its methods, windowClosing() is used to close the frame window.
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
public class AdapterExample {
// object of Frame
Frame f;
// class constructor
AdapterExample() {
// creating a frame with the title
f = new Frame ("Window Adapter");
// adding the WindowListener to the frame
// overriding the windowClosing() method
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
f.dispose();
}
});
// setting the size, layout and
f.setSize (400, 400);
f.setLayout (null);
f.setVisible (true);
}
// main method
public static void main(String[] args) {
new AdapterExample();
}
}
You can also try this code with Online Java Compiler
The MouseMotionAdapter class and its various methods are implemented in the following example to listen to mouse motion events in the Frame window.
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
// class which inherits the MouseMotionAdapter class
public class MouseMotionAdapterExample extends MouseMotionAdapter {
// object of Frame class
Frame f;
// class constructor
MouseMotionAdapterExample() {
// creating the frame with the title
f = new Frame ("Mouse Motion Adapter");
// adding MouseMotionListener to the Frame
f.addMouseMotionListener (this);
// setting the size, layout and visibility of the frame
f.setSize (300, 300);
f.setLayout (null);
f.setVisible (true);
}
// overriding the mouseDragged() method
public void mouseDragged (MouseEvent e) {
// creating the Graphics object and fetching them from the Frame object using getGraphics() method
Graphics g = f.getGraphics();
// setting the color of graphics object
g.setColor (Color.ORANGE);
// setting the shape of graphics object
g.fillOval (e.getX(), e.getY(), 20, 20);
}
public static void main(String[] args) {
new MouseMotionAdapterExample();
}
}
You can also try this code with Online Java Compiler
Java adapter classes are used by default to implement listener interfaces. We will not have to implement all listener interface functions if we inherit the adapter class. As a result, the code is saved.
What is the adapter class in Java?
An adapter class in Java provides default implementations for methods in listener interfaces, simplifying the creation of event handlers.
Differentiate between the Java Adapter class and Java Listener Interface.
An adapter class does not need to implement all of the methods in an interface. When only a handful of the interface's methods need to be modified, this method is utilized. A listener's interface must have all of the methods defined in it implemented.
What is an adapter and types?
An adapter is a design pattern that allows interfaces of incompatible objects to work together. Types include class, object, and interface adapters.
Conclusion
In this article, we have extensively discussed the Java adapter classes. We began with a brief introduction to Java adapter classes, followed by a detailed explanation and key examples.