Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

April 9, 2016 Categories: Event Handling. No Comments 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.

 

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.

 

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

 

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 *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me