Introduction to Java Adapter Classes
Adapter Classes in Java are nothing new. They are used to provide the implementation of Listener interfaces. The advantage of the adapter class is that it saves code. If we inherit the adapter class using the Adapter interface, we will not be forced to provide the implementation of all the methods of the listener interface. There are namely three packages in which the Java adapter classes are found. The three packages are:
- java.awt. event
- java.awt. dnd
- java.swing. event
Also, another advantage of the Adapter Class is that they are useful when we want to process a few of the events that are handled by a particular event listener interface.
Working of Adapter Classes in Java with Syntax
As we have noticed in the introduction, there are three packages where we find the adapter classes. We are now going to see the corresponding Listener interface of the respective adapter classes in all the three packages.
- java.awt. event Adapter Classes
- java.awt.dnd Adapter Classes
- java.swing. event Adapter Classes
1. java.awt. event Adapter Classes
Below are some of the Adapter classes and their corresponding listener interface. These are present in the Abstract Window Toolkit of the Java package.
The below code gives the syntax of the Adapter Class and it gives us an idea as to how the WindowAdapter Class looks as well as the syntax.
f=new Frame("Hello World");
public void windowClosing(WindowEvent e)
4.8 (4,347 ratings)
The below code gives the syntax of the KeyAdapter Class.
public abstract keyAdapter Class
The below code gives the syntax of the MouseAdapter Class.
f=new Frame("Mouse Adapter");
The below code gives the syntax of the MouseMotionAdapter Class.
f=new Frame("Adapter for Mouse Motion");
public void mouseDragged(MouseEvent e)
The below code gives the syntax of the FocusAdapter class.
public abstract class FocusAdapter
The below code gives the syntax of the ComponentAdapter class.
class MyAdapter extends ComponentAdapter
public void componentMoved(ComponentEvent e)
int a = e.getComponent().getX();
int b = e.getComponent().getY();
System.out.println("Value of X " + a);
System.out.println("Value of Y: " + b);
The below code gives the syntax of the ContainerAdapter Class.
public abstract class ContainerAdapter
The below code gives the syntax of the HierarchyBoundsAdapter class.
public abstract class HierarchyBoundsAdapter
2. java.awt.dnd Adapter Classes
Below are some of the adapter classes which are used in the awt.dnd package. They are listed down in a table.
|Adapter Class||Listener Interface|
In this case, there are two Adapter classes under the respective package which is called. We are going to see the syntax of the two Adapter classes and study the codes which are under the respective package.
The below code gives the syntax of the DragSourceAdapter class.
public abstract class DragSourceAdapter
The below code gives the syntax of the DragTargetAdapter class.
public abstract class DragTargetAdapter
3. java.swing. event Adapter Classes
Below are some of the classes which are used in the swing. event package. They are listed down in a table.
|Adapter Class||Listener Interface|
In this case, we notice two adapter classes along with their Listener Interface. The package javax. swing. event has two adapter classes which are respectively listed down in the table.
Below is the code which provides the syntax of the MouseInputAdapter class.
public abstract class MouseInputAdapter
Below is the code which provides the syntax of the InternalFrameAdapter class.
public abstract class InternalFrameAdapter
Adapter Classes are basically used in the case when the class is declared as abstract. Then only the working is based on different functionalities which include the working of the Java classes which are part of different Java packages. There are namely three packages in Java which has different Adapter classes as well as Listener Interfaces along with them. Adapter Classes are used in numerous cases where there is the usage of servers and there is connectivity with the SQL database.
There are different functionalities of Java Adapter classes and mostly all of them are being used in Android programming as well. Adapter Classes are well-known functionality in Java programming which helps coders get the idea of many applications that can be used via Swing methodology. There are many ideas that can be put forward to develop the Adapter Class methodology in Java. Loops can also be incorporated to develop some other types of functionality.
In this article, we see the working of Java adapter classes and we see that there are three main packages that contain the adapter classes. We see the syntax of all the adapter classes which are present, as well as some of the codes of the working, are also provided. Adapter classes are basically unique in Java and they are part of Swing functionality in Java.
This is a guide to Java Adapter Classes. Here we discuss adapter classes and their corresponding listener interface along with working of adapter classes with syntax. You may also look at the following articles to learn more –