0% found this document useful (0 votes)
22 views7 pages

3.2.1 Action Event

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

3.2.1 Action Event

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

Action Event & Action Listener

ActionEvent-
-defined in java.awt.event package.
-The ActionEvent is generated when button is clicked or the item of a list is double clicked.
Constructors-
1. ActionEvent(Object source, int id, String command)- Constructs an ActionEvent object.
2. ActionEvent(Object source, int id, String command, int modifiers) - Constructs an
ActionEvent object with modifier keys.
3. ActionEvent(Object source, int id, String command, long when, int modifiers)- Constructs an
ActionEvent object with the specified modifier keys and timestamp
Methods-
1. String getActionCommand()- Returns the command string associated with this action.
ActionListener-
-interface is found in java.awt.event
-used to receive action event.
-the respective class which wants to handle action event implements this interface, and the object
created with that class is registered with a component using the components addActionListener method
i.e register listener syntax- compobj.addActionListener(Eventclass e)
-When the action event occurs that object action performed method is invoked.

Method-
1. public abstract void actionPerformed(ActionEvent e)-
-The actionPerformed() method is invoked automatically whenever you click on the registered
component.
Develop a program to implement action event
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class action extends Applet implements ActionListener
{
Button b1; Label l1;
public void init()
{
b1=new Button("Ok");
l1=new Label("Press Ok button");
setLayout(new FlowLayout(FlowLayout.RIGHT));
add(l1); add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
l1.setText("U Have pressed ok button");
}
}
/* <applet code=action.class height=300 width=300> </applet> */
Develop a program to implement action event
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class action121 extends Applet implements ActionListener
{
Button b1; String s;
public void init()
{
b1=new Button("Ok");
setLayout(new FlowLayout(FlowLayout.RIGHT));
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{ s="U Have pressed ok button"; }}
public void paint(Graphics g)
{ g.drawString(s,10,20); }
}

/* <applet code=action121.class height=300 width=300> </applet> */


Develop a program to implement action event
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class action12 extends Applet implements ActionListener
{
Button b1,b2; Label l1;
public void init()
{
b1=new Button("Ok"); b2=new Button("Cancel");
l1=new Label("Press button");
setLayout(new FlowLayout(FlowLayout.RIGHT));
add(l1); add(b1); add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{ if (e.getSource()==b1)
{ l1.setText("U Have pressed ok button"); }
if(e.getSource()==b2)
{ l1.setText("U Have pressed cancel button"); }
}
} /* <applet code=action12.class height=300 width=300> </applet> */
Develop a program to implement action event
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class action12 extends Applet implements ActionListener
{
Button b1,b2; Label l1;String s;
public void init()
{
b1=new Button("Ok"); b2=new Button("Cancel");
l1=new Label("Press any button");
setLayout(new FlowLayout(FlowLayout.RIGHT));
add(l1);add(b1); add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{ s=e.getActionCommand();
if(s.equals("Ok")); { l1.setText("U Have pressed ok button"); }
if(s.equals("Cancel")) { l1.setText("U Have pressed cancel button"); }
}
}
/* <applet code=action12.class height=300 width=300> </applet> */
Develop a program to implement action event
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class addi extends Applet implements ActionListener
{
Label l1,l2,l3; TextField t1,t2,t3; Button b; int x,y,z;
public void init()
{
l1 = new Label("mark-1"); t1 = new TextField(10); l2 = new Label("mark-2");
t2 = new TextField(10); l3 = new Label("total"); t3 = new TextField(10);
b = new Button("add");
b.addActionListener(this);
add(l1); add(t1); add(l2); add(t2); add(l3); add(t3); add(b);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource() == b) {
x = Integer.parseInt(t1.getText());
y = Integer.parseInt(t2.getText());
z = x+y;
t3.setText(String.valueOf(z));
}}}
/* <applet code = "addi.class" width = 100 height = 200> </applet> */

You might also like