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. 1 Comment on Using Delegation Event Model

In this article we will learn about using delegation event model i.e., how to implement event handling in Java programs.

 

Following are the basic steps in using delegation event model or for handling events in a Java program:

  1. Implement the appropriate listener interface.
  2. Register the listener with the source.
  3. Provide appropriate event handler to handle the event raised on the source.

 

Key Events Handling

 

Following is a Java program which handles key events. In the program when the user types characters in the text field, they are displayed in a label below the text field.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyApplet extends JApplet implements KeyListener
{
	JTextField jtf;
	JLabel label;
	public void init()
	{
		setSize(600,300);
		setLayout(new FlowLayout());
		jtf = new JTextField(20);
		add(jtf);
		jtf.addKeyListener(this);
		label = new JLabel();
		add(label);
	}
	public void keyPressed(KeyEvent ke){}
	public void keyReleased(KeyEvent ke){}
	public void keyTyped(KeyEvent ke)
	{
		label.setText(String.valueOf(ke.getKeyChar()));
	}
}

 

Initial output of the above program is as follows:

 

key-event1

 

After the user enters a character into the text field, the same character is displayed in the label beside the text field as shown in the below image:

 

key-event2

 

 

Mouse Events Handling

 

Following is a Java program which handles both mouse events and mouse motion events. When the user performs a mouse event on the applet it is updated in the label.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class MyApplet extends JApplet implements MouseListener, MouseMotionListener
{
	JLabel label;
	public void init()
	{
		setSize(600,300);
		setLayout(new FlowLayout());
		label = new JLabel();
		add(label);
		addMouseListener(this);
		addMouseMotionListener(this);
	}
	public void mouseClicked(MouseEvent me)
	{
		label.setText("Mouse is clicked");
	}
	public void mousePressed(MouseEvent me){}
	public void mouseReleased(MouseEvent me){}
	public void mouseEntered(MouseEvent me){}
	public void mouseExited(MouseEvent me){}
	public void mouseDragged(MouseEvent me){}
	public void mouseMoved(MouseEvent me)
	{
		label.setText("Mouse is moved");
	}
}

 

Initial output of the above program is as shown below:

 

mouse-event1

 

When the user moves the mouse cursor over the applet display area, then the output is as shown below:

 

mouse-event2

 

When the user clicks on the applet display area, the output is as shown below:

 

mouse-event3

 

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.

1 Comment

You can follow any responses to this entry through the RSS 2.0 feed.

I’m really impressed together with your writing skills as neatly as with the layout in your weblog.
Is this a paid subject or did you customize it your self?

Either way stay up the excellent high quality writing, it is rare
to look a great weblog like this one these days..

Leave a Reply

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