0% found this document useful (0 votes)
18 views6 pages

Unit-04 Note-05

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Unit-04 Note-05

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Event handling in java:

An event is an object that describes a change of state in a


source. It is generated when a person interacts with the GUI. Some
of the actions that generates the events are pressing a button,
entering a character through keyboard, selecting an item from a list,
clicking the mouse, etc..

In Java, a source generates an event and sends it to one or more


listeners. The listener simply waits until it receives an event. Once
an event is received, the listener processes the event and then
returns. The listeners must register with a source in order to receive
an event notification.

Events:-

An event is an action that occurs upon any controls like button,


list box, text box , etc. It can be generated when a person interacts
with the elements in a GUI.

Event sources:-

A source is an object that generates an event(eg: Button). A


source must register with listeners in order for the listeners to
receive notifications about a specific type of event.

Event listeners:-

A listener is an object that is notified when an event occurs. To


handle different types of events, the number of listener interfaces are
used in Java. These interfaces are included in java.awt.event
package. Fore example, the ActionListener interface is notified
when an action event occurs.
Event listener interfaces:-

1. ActionListener
2. ItemListener
3. TextListener
4. KeyListener
5. MouseListener

ActionListener

This interface defines the actionPerformed() method. This


method is called when an action event occurs. It takes the following
form:
public void actionPerformed(ActionEvent ae)
{
// code to handle action event
}
ItemListener:

This interface defines the itemStateChanged() method. It is


invoked when an item is selected from a list of items. It takes the form:
public void itemStateChanged(ItemEvent ie)
{
// code to handle item event
}
TextListener:

This interface defines the textValueChanged() method. It is


invoked when a change occurs in a text boxes. It takes the form:
public void textValueChanged(TextEvent te)
{
// Code to handle text change event
}
KeyListener:

This interface defines three methods, keyPressed(),


keyTyped() and keyReleased(). These methods are called when a
key is pressed, typed or released. It takes the forms:

public void keyPressed(KeyEvent ke)


{
// Code to handle key event
}
public void keyTyped(KeyEvent ke)
{
// Code to handle key event
}
public void keyReleased(KeyEvent ke)
{
// Code to handle key event
}

MouseListener:

This interface defines five methods. If the mouse is pressed


and released at the same point, mouseClicked() is called. When the
mouse enters upon a component, the mouseEntered() method is
called. When it leaves, mouseExited() is called. The mousePressed()
and mouseReleased() methods are invoked when the mouse is
pressed and released. The general forms are:

public void mouseEntered(MouseEvent me)


{
// Code to handle mouse event
}
public void mousePressed(MouseEvent me)
{
// Code to handle mouse event
}
public void mouseReleased(MouseEvent me)
{
// Code to handle mouse event
}
public void mouseClicked(MouseEvent me)
{
// Code to handle mouse event
}
public void mouseExited(MouseEvent me)
{
// Code to handle mouse event
}

AWT program for Button event

//Program to add two numbers


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Demo extends Applet implements ActionListener
{

Label lb1=new Label("Enter first number:");


Label lb2=new Label("Enter second number:");
Label lb3=new Label("Sum is:");
TextField t1=new TextField(20);
TextField t2=new TextField(20);
TextField t3=new TextField(20);

Button b1=new Button("SUM");

public void init()


{
add(lb1);
add(t1);
add(lb2);
add(t2);
add(lb3);
add(t3);
add(b1);
b1.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)


{
int a,b,c;
String s1,s2;
s1=t1.getText();
s2=t2.getText();
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=a+b;
t3.setText(Integer.toString(c));
}
}
AWT program for keyboard events

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Demo extends Applet implements KeyListener
{
Button b1=new Button("SUBMIT");
public void init()
{
add(b1);
b1.addKeyListener(this);
}

public void keyPressed(KeyEvent ke)


{
System.out.println(“Key pressed”);
}
public void keyTyped(KeyEvent ke)
{
System.out println(“Key typed”);
}
public void keyReleased(KeyEvent ke)
{
System.out.println(“Key released”);
}
}

You might also like