CHP_3 Event Handling (2) (1)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Chapter No:03

Event Handling

Prepared by,

Mrs. Suwarna T.
The Delegation Event Model

The modern approach to handling events

is based on the delegation event model,


which defines standard and consistent
mechanisms to generate and process
events
Events

In the delegation model, an event is an object


that describes a state change in a source. It
can be generated as a consequence of a person
interacting the elements in a graphical user
interface.
Event Sources

A source is an object that generates an event. This

occurs when the internal state of that object


changes in some way. Sources may generate more
than one type of event. A source must register
listeners in order for the listeners to receive
notifications
Event Sources
 Each type of event has its own registration
method.Here is the general form:

public void addTypeListener(TypeListener


el)
Here, Type is the name of the event and el is
a reference to the event listener.
Event Listeners
 A listener is an object that is notified when an event occurs.

It has two major requirements. First, it must have been


registered with one or more sources to receive notifications
about specific types of events. Second, it must implement
methods to receive and process these notifications.The
methods that receive and process events are defined in a
set of interfaces found in java.awt.event.
Event Classes

At the root of the Java event class hierarchy is


EventObject, which is in java.util.
It is the superclass for all events. Its one
constructor is shown here:
EventObject(Object src)
Here, src is the object that generates this event.
Event Classes
EventObject contains two methods:
getSource( ) :The getSource( )method returns the source
of the event.
Syntax is:
Object getSource( )

toString( ) returns the string equivalent of the event.


getActionCommand():-
String getActionCommand()
Event Classes
The class AWTEvent, defined within the java.awt package, is
a subclass of EventObject. It is the superclass (either directly
or indirectly) of all AWT-based events

Its getID( ) method can be used to determine the type of the


event. The signature of this method is shown here:

int getID( )
Event Classes
EventObject is a superclass of all events.

AWTEvent is a superclass of all AWT

events that are handled by the delegation


event model.
Types of Events
Event Class Description

ActionEvent Generated when a button is pressed, a list item is


double-clicked or Menu item is
selected.

AdjustmentEvent Generated when a scroll bar is manipulated.

ComponentEvent Generated when a component is hidden, moved,


resized,or becomes visible
ContainerEvent Generated when a component is added to or
removed
Event Class Description

FocusEvent Generated when a component gains or loses keyboard


focus.

ItemEvent Generated when a check box or list item is clicked also


occurs when a choice
selection is made or a checkable menu item is selected or
deselected.
KeyEvent Generated when input is received from the keyboard.

MouseEvent Generated when the mouse is dragged, moved, clicked,


pressed, or released; also
generated when the mouse enters or exits a component
Event Listener Interfaces

Event Listener Interfaces

Event Listener Interfaces

Event Listener Interfaces

Event Listener Interfaces

Event Listeners Interfaces

The delegation event model has two parts:


 sources

 listeners.

Listeners are created by implementing one or more of the interfaces


defined by the java.awt.event package. When an event occurs, the
event source invokes the appropriate method defined by the listener
and provides an event object as its argument.
Types of Listeners
Interface Description

ActionListener Defines one method to receive action events.


This interface defines the actionPerformed( )
method that is invoked when an action event occurs.
void actionPerformed(ActionEvent ae)

AdjustmentListener This interface defines the


adjustmentValueChanged( ) method that is invoked
when an adjustment event occurs. Its general form is
shown here:
void adjustmentValueChanged(AdjustmentEvent ae)
Types of Listener
Interface Description

ItemListen Defines one method to recognize when the state of an


er item changes. This interface defines the
itemStateChanged( ) method that is invoked when the
state of an item changes. Its general form is shown
here:
void itemStateChanged(ItemEvent ie)
KeyListen Defines three methods to recognize when a key is
er pressed, released, or
typed.
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
Types Of Listener
ComponentListen void
er componentResized(ComponentEvent ce)
void componentMoved(ComponentEvent
ce)
void componentShown(ComponentEvent
ce)
void componentHidden(ComponentEvent
ce)

TextListener Defines one method to recognize when a text


value changes.
void textChanged(TextEvent te)
MouseListener It defines five methods.
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)

MouseMotionListen It defines two methods.


er void mouseDragged(MouseEvent me)
void mouseMoved(MouseEvent me)
Program using ActionEvent
import java.awt.*; public void actionPerformed(ActionEvent
import java.awt.event.*;
import java.applet.*;
ae)
/*<applet code= action_back1.class width=200 height=200> {
</applet>*/
if(ae.getSource()==b1)
public class action_back1 extends Applet implements {
ActionListener
{
setBackground(Color.red);
Button b1,b2; }
public void init()
{
else
b1=new Button("ok"); if(ae.getSource()==b2)
b2=new Button("no");
{
add(b1);add(b2); setBackground(Color.blue);
b1.addActionListener(this);
}
b2.addActionListener(this);
repaint();
} }}
Program using Action Event
import java.awt.*; public void actionPerformed(ActionEvent ae)
import java.awt.event.*; { String str = ae.getActionCommand();
import java.applet.*; if(str.equals("Yes"))
/* <applet code="button_event" width=250 { msg = "You pressed Yes.";
height=150>
} else if(str.equals("No"))
</applet> */
{ msg = "You pressed No.";
public class button_event extends Applet
} else
implements ActionListener
{ String msg = "";
{ msg = "You pressed Undecided.";
Button yes, no, maybe; }
public void init() repaint();
{ yes = new Button("Yes"); }
no = new Button("No"); public void paint(Graphics g)
maybe = new Button("Undecided"); { g.drawString(msg, 6, 100);
add(yes); add(no); add(maybe); }
yes.addActionListener(this); }
no.addActionListener(this);
Program Using ItemEvent
import java.awt.*; public void itemStateChanged(ItemEvent ie)
import java.awt.event.*;
{
import java.applet.*;
public class check_event extends Applet implements repaint();
ItemListener }
{
public void paint(Graphics g)
Checkbox c1;
public void init() {
{ g.drawString("WIN/XP"+c1.getState(),40,80);
c1=new Checkbox("win/xp",true); g.drawString("win/xp"+c1.getLabel(),60,100);
Checkbox c2=new Checkbox("win/NT");
}
Checkbox c3=new Checkbox("win/PROF");
Checkbox c4=new Checkbox("win/UNIX"); }
add(c1);add(c2);add(c3);add(c4); /*<applet code= check_event width=200
c1.addItemListener(this); height=200>
c2.addItemListener(this); </applet>*/
c3.addItemListener(this);
c4.addItemListener(this);
}
import java.awt.*;
public void itemStateChanged(ItemEvent ie)
import java.applet.*;
{
import java.awt.event.*;
public class radio_event extends Applet implements if(ie.getSource()==m1)
ItemListener tf.setText(m1.getLabel());
{ else
Checkbox m1,m2;
if(ie.getSource()==m2)
CheckboxGroup cbg;
TextField tf;
tf.setText(m2.getLabel());
public void init() }}
{setLayout(new FlowLayout(FlowLayout.LEFT)); /*<applet code=radio_event height=150
cbg=new CheckboxGroup(); width=150>
m1=new Checkbox("Information
Technilogy",cbg,true);
</applet>*/
m2=new Checkbox("Computer",true,cbg);
tf=new TextField(30);
add(m1);add(m2);add(tf);
m1.addItemListener(this);
m2.addItemListener(this);
Program Using RadioButton
import java.awt.*; public void itemStateChanged(ItemEvent ie)
import java.applet.*;
import java.awt.event.*; {
public class radio_eventcolor extends Applet implements if(ie.getSource()==m1)
ItemListener
{ setBackground(Color.red);
Checkbox m1,m2,m3; else
CheckboxGroup cbg;
public void init() if(ie.getSource()==m2)
{setLayout(new FlowLayout(FlowLayout.LEFT)); setBackground(Color.green);
cbg=new CheckboxGroup();
m1=new Checkbox("RED",cbg,true);
else
m2=new Checkbox("GREEN",true,cbg); setBackground(Color.blue);
m3=new Checkbox("BLUE",true,cbg);
add(m1);
add(m2); }}/*<applet code=radio_eventcolor
add(m3); height=150 width=150>
m1.addItemListener(this);
m2.addItemListener(this);
m3.addItemListener(this); </applet>*/
}
Program Using TextEvent
import java.awt.*; public void actionPerformed(ActionEvent ae)
import java.applet.*;import {
java.awt.event.*; repaint();
public class text12 extends Applet }
implements ActionListener public void paint(Graphics g)
{String str=t1.getSelectedText();
{TextField t1,t2;
g.drawString("selected text"+str,20,90);
public void init()
g.drawString("getText"+t1.getText(),20,110);
{Label l1=new Label("enter name"); g.drawString("getText"+t2.getText(),20,130);
Label l2=new Label("Password"); }
t1=new TextField(20); }
t2=new TextField(20);t2.setEchoChar('*');
add(l1);add(t1);add(l2);add(t2); /*<applet code=text12 width=200
height=200>
t1.addActionListener(this);
</applet>*/
t2.addActionListener(this);
Program Using TextEvent
import java.awt.*; add(l1);add(t1);add(l2);add(t2);
t1.addTextListener(this);
import java.applet.*; t2.addTextListener(this);
import java.awt.event.*;
}
public void textValueChanged(TextEvent te)
public class text12event extends Applet {
repaint();
implements TextListener }
{ public void paint(Graphics g)
{
TextField t1,t2; String str=t1.getSelectedText();
public void init() g.drawString("getText:"+t1.getText(),20,110);
g.drawString("selected text:"+str,20,130);
{
g.drawString("getText:"+t2.getText(),20,150);
Label l1=new Label("enter name"); }
Label l2=new Label("Password"); }

t1=new TextField(20); /*<applet code=text12event width=200 height=200>


t2=new TextField(20); </applet>*/

t2.setEchoChar('*');
Program to get the labelof checkbox in text field
import java.awt.*;
import java.awt.event.*; public void
import java.applet.*;
/* <applet code="check1" height=200 width=250> itemStateChanged(ItemEvent
</applet>
*/
ie)
public class check1 extends Applet implements ItemListener
{ {
Checkbox c1,c2;
TextField t; if(ie.getSource()==c1)
public void init()
{ t.setText(c1.getLabel());
c1=new Checkbox("IT");
c2=new Checkbox("CO"); else
t=new TextField();
add(c1);
add(c2);
t.setText(c2.getLabel());
add(t);
c1.addItemListener(this);
c2.addItemListener(this);
} }
Exchange The String in TextField
import java.awt.*; public void actionPerformed(ActionEvent ae)
import java.awt.event.*; { String t=t1.getText();
import java.applet.*;
t1.setText(t2.getText());
/* <applet code="button_event_exchange" width=250
height=150> t2.setText(t);
</applet> */ }
public class button_event_exchange extends Applet }
implements ActionListener
{
TextField t1,t2;
Label l1,l2;
Button b;
public void init()
{ t1=new TextField(20);
t2=new TextField(20);
l1=new Label("information");
l2=new Label("technology");
b=new Button("ok");
add(l1);add(t1);
add(l2);add(t2);
add(b);
Program Using Action Event public void actionPerformed(ActionEvent ae)
import java.awt.*;
{
import java.applet.*;
int no;
import java.awt.event.*;
int n=Integer.parseInt(t1.getText());
public class sqcube_event extends Applet implements
ActionListener if(ae.getSource()==b1)
{ {
TextField t1,t2; no=n*n;
Label l1,l2; l2.setText("square is");
Button b1,b2; t2.setText(Integer.toString(no));
public void init() }
{
else
setLayout(new GridLayout(3,2));
{
l1=new Label("enter no");
no=n*n*n;
l2=new Label();
t1=new TextField(15); l2.setText("cube is");
t2=new TextField(15); t2.setText(Integer.toString(no));
b1=new Button("square"); }
b2=new Button("cube"); }
add(l1);add(t1); }
add(l2);add(t2);
add(b1);add(b2); /*<applet code=sqcube_event width=200
b1.addActionListener(this); height=200>
b2.addActionListener(this);
Output

You might also like