Unit 2: Even handling

5th Semester

Event Handling

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

Components of Event Handling

Event handling has three main components,

  • Events : An event is a change in 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.

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 process the event and then return. Events are supported by a number of Java packages, like java.utiljava.awt and java.awt.event.

Important Event Classes and Interface

Event Classes Description Listener Interface
ActionEvent generated when button is pressed, menu-item is selected, list-item is double clicked ActionListener
MouseEvent generated when mouse is dragged, moved,clicked,pressed or released and also when it enters or exit a component MouseListener
KeyEvent generated when input is received from keyboard KeyListener
ItemEvent generated when check-box or list item is clicked ItemListener
TextEvent generated when value of textarea or textfield is changed TextListener
MouseWheelEvent generated when mouse wheel is moved MouseWheelListener
WindowEvent generated when window is activated, deactivated, deiconified, iconified, opened or closed WindowListener
ComponentEvent generated when component is hidden, moved, resized or set visible ComponentEventListener
ContainerEvent generated when component is added or removed from container ContainerListener
AdjustmentEvent generated when scroll bar is manipulated AdjustmentListener
FocusEvent generated when component gains or loses keyboard focus FocusListener

 

Steps to handle events:

  1. Implement appropriate interface in the class.
  2. Register the component with the listener.

Registration Methods

For registering the component with the Listener, many classes provide the registration methods. For example:

  • Button
    • public void addActionListener(ActionListener a){}
  • MenuItem
    • public void addActionListener(ActionListener a){}
  • TextField
    • public void addActionListener(ActionListener a){}
    • public void addTextListener(TextListener a){}
  • TextArea
    • public void addTextListener(TextListener a){}
  • Checkbox
    • public void addItemListener(ItemListener a){}
  • Choice
    • public void addItemListener(ItemListener a){}
  • List
    • public void addActionListener(ActionListener a){}
    • public void addItemListener(ItemListener a){}

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>
</applet>

2.7 Java Event Handling Code

We can put the event handling code into one of the following places:

  1. Within class
  2. Other class
  3. Anonymous class

Java event handling by implementing ActionListener

  1. import java.awt.*;
    import java.awt.event.*;
    class AEvent extends Frame implements ActionListener{
    TextField tf;
    AEvent(){
    //create components
    tf=new TextField();
    tf.setBounds(60,50,170,20);
    Button b=new Button("click me");
    b.setBounds(100,120,80,30);
    //register listener
    b.addActionListener(this);//passing current instance
    //add components and set size, layout and visibility
    add(b);add(tf);
    setSize(300,300);
    setLayout(null);
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e){
    tf.setText("Welcome");
    }
    public static void main(String args[]){
    new AEvent();
    }
    }

     

public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above example that sets the position of the component it may be button, textfield etc.
event handling in java


2) Java event handling by outer class

import java.awt.*;
import java.awt.event.*;
class AEvent2 extends Frame{
TextField tf;
AEvent2(){
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
Outer o=new Outer(this);
b.addActionListener(o);//passing outer class instance
//add components and set size, layout and visibility
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent2();
}
}
import java.awt.event.*;
class Outer implements ActionListener{
AEvent2 obj;
Outer(AEvent2 obj){
this.obj=obj;
}
public void actionPerformed(ActionEvent e){
obj.tf.setText("welcome");
}
}

 


3) Java event handling by anonymous class

import java.awt.*;
import java.awt.event.*;
class AEvent3 extends Frame{
TextField tf;
AEvent3(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(50,120,80,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(){
tf.setText("hello");
}
});
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
new AEvent3();
}
}