Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Event Handling. No Comments on Adapter Classes

In this article we will learn about adapter classes which are used to simplify the event handling process. Sample Java programs are also provided.

 

Adapter classes are a set of classes which can be used to simplify the event handling process. As we can see in the mouse event handling program here, even though we want to handle only one or two mouse events, we have to define all other remaining methods available in the listener interface(s).

 

To remove the aforementioned disadvantage, we can use adapter classes and define only the required event handling method(s). Some of the adapter classes available in the java.awt.event package are listed below:

 

adapter-classes

 

So, the mouse event handling program can be simplified using adapter classes as follows:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyApplet extends JApplet
{
	JLabel label;
	public void init()
	{
		setSize(600,300);
		setLayout(new FlowLayout());
		label = new JLabel();
		add(label);
		addMouseListener(new MyAdapter(label));
	}
	
}
class MyAdapter extends MouseAdapter
{
	JLabel label;
	MyAdapter(JLabel label)
	{
		this.label = label;
	}
	public void mouseClicked(MouseEvent me)
	{
		label.setText("Mouse is clicked");
	}
}

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *