CHP_3 Event Handling (2) (1)
CHP_3 Event Handling (2) (1)
CHP_3 Event Handling (2) (1)
Event Handling
Prepared by,
Mrs. Suwarna T.
The Delegation Event Model
int getID( )
Event Classes
EventObject is a superclass of all events.
listeners.
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