Home> Java> javaTutorial> body text

Java Adapter Classes

PHPz
Release: 2024-08-30 16:07:15
Original
333 people have browsed it

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 implement all the listener interface methods. There are namely three packages in which the Java adapter classes are found. The three packages are:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • 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 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.

Adapter Class

Listener Interface

WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
HierarchyBoundsAdapter HierarchyBoundsListener
Adapter Class

Listener Interface

WindowAdapter WindowListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
FocusAdapter FocusListener
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
HierarchyBoundsAdapter HierarchyBoundsListener
WindowAdapter ClassThe below code gives the syntax of the Adapter Class, and it gives us an idea as to how the WindowAdapter Class looks and the syntax.

Syntax:

AdapterExample() { f=new Frame("Hello World"); f.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) { f.close(); } }
Copy after login
KeyAdapter ClassThe below code gives the syntax of the KeyAdapter Class.

Syntax:

public abstract keyAdapter Class extends Object Implements KeyListener
Copy after login
MouseAdapter ClassThe below code gives the syntax of the MouseAdapter Class.

Syntax:

MouseAdapterExample() { f=new Frame("Mouse Adapter"); f.addMouseListener(this); f.setSize(100,100); f.setLayout(null); f.setVisible(false); }
Copy after login
MouseMotionAdapter ClassThe below code gives the syntax of the MouseMotionAdapter Class.

Syntax:

MouseMotionAdapterExample() { f=new Frame("Adapter for Mouse Motion"); f.addMouseMotionListener(this); f.setSize(100,100); f.setLayout(null); f.setVisible(false); } public void mouseDragged(MouseEvent e) { Graphics g=f.getGraphics(); g.setColor(Color.ORANGE); g.fillOval(e.getX(),e.getY(),20,20); }
Copy after login
FocusAdapter ClassThe below code gives the syntax of the FocusAdapter class.

Syntax:

public abstract class FocusAdapter extends Object Implements FocusListener
Copy after login
ComponentAdapter ClassThe below code gives the syntax of the ComponentAdapter class.

Syntax:

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); } }
Copy after login
ContainerAdapter ClassThe below code gives the syntax of the ContainerAdapter Class.

Syntax:

public abstract class ContainerAdapter extends Object implements ContainerListener
Copy after login
HierarchyBoundsAdapter ClassThe below code gives the syntax of the HierarchyBoundsAdapter class.

Syntax:

Adapter Class Listener Interface
DragSourceAdapter DragSourceListener
DragTargetAdapter DragTargetListener
public abstract class HierarchyBoundsAdapter extends Object implements HierarchyBoundsListener
Copy after login
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
DragSourceAdapter DragSourceListener
DragTargetAdapter DragTargetListener

In this case, there are two Adapter classes under the respective package, which is called. We will see the syntax of the two Adapter classes and study the codes under the respective package.

DragSourceAdapter Class

The below code gives the syntax of the DragSourceAdapter class.

Syntax:

public abstract class DragSourceAdapter extends Object implements DragSourceAdapterListener
Copy after login
DragTargetAdapter Class

The below code gives the syntax of the DragTargetAdapter class.

Syntax:

public abstract class DragTargetAdapter extends Object implements DragTargetAdapterListener
Copy after login

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
MouseInputAdapter MouseInputListener
InternalFrameAdapter InternalFrameListener

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.

MouseInputAdapter Class

Below is the code which provides the syntax of the MouseInputAdapter class.

Syntax:

public abstract class MouseInputAdapter extends MouseAdapter implements MouseInputListener
Copy after login
InternalFrameAdapter Class

Below is the code which provides the syntax of the InternalFrameAdapter class.

Syntax:

public abstract class InternalFrameAdapter extends Object implements InternalFrameListener
Copy after login

Explanation

Adapter Classes are basically used in the case when the class is declared as abstract. Only the working is based on different functionalities, including the working of the Java classes, which are part of different Java packages. There are three packages in Java that have different Adapter classes and Listener Interfaces along with them. Adapter Classes are used in numerous cases where servers are used, and there is connectivity with the SQL database.

There are different functionalities, 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.

Conclusion

In this article, we see the working, 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.

The above is the detailed content of Java Adapter Classes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!