EVENT HANDLING

Any program that uses GUI (graphical user interface) such as Java application written for windows, is event driven. Event describes the change of state of any object. 
Example : Pressing a button, Entering a character in Textbox.

Components of Event Handling

Event handling has three main components,
  • Events : An event is a change of state of an object.
  • Events Source : Event source is an object that generates an event.
  • Listeners : A listener is an object that listens to the event. A listener gets notified when an event occurs.
Image result for event handling in java

How Events are handled ?

A source generates an Event and send it to one or more listeners registered with the source. Once event is received by the listener, they processe the event and then return. Events are supported by a number of Java packages, like java.utiljava.awt and java.awt.event.

Important Event Classe and Interface


Image result for delegation event model in java

Example of Event Handling

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class Test extends Applet implements KeyListener
{
 String msg="";
 public void init()
 {
   addKeyListener(this);       
 }
 public void keyPressed(KeyEvent k)
 {
   showStatus("KeyPressed");
 }
 public void keyReleased(KeyEvent k)
 {
   showStatus("KeyRealesed");
 }
 public void keyTyped(KeyEvent k)
 {
   msg = msg+k.getKeyChar();
   repaint();
 }
 public void paint(Graphics g)
 {
   g.drawString(msg, 20, 40);
 }
}
HTML code :
< applet code="Test" width=300, height=100 >


event handling example

2 comments:

  1. Join Now Java course , Online Java course
    https://corosocial.com/read-blog/141413_future-of-java-technology-will-help-you-get-there.html
    APTRON provides the best Java course , Online Java course for beginners as well as advanced programmers. Java course, Online Java course will help you to learn and nourish your programming skills in Java.

    ReplyDelete