import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="actionapplet" width=500 height=600></applet>*/
public class actionapplet extends Applet implements ActionListener{
  String msg,str;
  Button b1,b2,b3;

 public void init(){

      b1=new Button("substract");
      b2=new Button("add");
    public void actionPerformed(ActionEvent ae)
                     msg="addition is to be performed";
                       msg="subtraction is to be performed";
      public void paint(Graphics g)

My doubt is :in the above code
in the sentence b1.addActionListener(this) what does this signify
What actualy is present there?
i read it in many books but i am unable to get a clear picture.
can anyone help me .


This statement calls the addActionListener() method for the object b1. The reference to the action listener is saved in a list.
Object b1 must be of class that can receive Action Events, like a key press.

When that object b1 receives an Action Event, it looks in its list of listeners for Action Events and calls any listeners in the list.
The posted code passes "this" as a listener. The object refered to by "this" must be of type ActionListener. (coded by 'implements ActionListener' for the class def).


isn't object b1 an event source???
and i know that this must be an object implementing actionListener interface
. My doubt is : How can we replace this with an actual argument in a clearer way.

this must be a reference of an object implementing actionListener interface.
And if so , in the above case why cant we replace the same sentence as:

b1.addActionListener(new actionapplet());

How can we replace this with an actual argument in a clearer way

"this" is pretty clear and is used in lots of programs.
Another way is to create a new class that implements ActionListener, create an instance of that object and pass a reference to it in the add call.

why cant we replace the same sentence as:

b1.addActionListener(new actionapplet());

This would create a SECOND instance of the actionapplet class. Now you have two with the exactly same code?

but you said that another way to do the same is to create an instance of class implementing Actionlistener and pass it as an argument.
This works fine in general.
But how to do such kind of replacement when using applets.
Now in the above this must be a reference of the actionapplet class i think.
Then what difference does it make in replacing this with an instance of actionapplet..

As you said if doing so results in generation of another instance of actionapplet class , then wouldn't using this result in the generation of a second instance of actionapplet class?

this must be a reference of the actionapplet class

No. The arg to addActionListener() must be an instance of an ActionListener.
Because the actionapplet class implements ActionListener it is the type: ActionListener. Hence you can use "this" as the arg to addActionListener()

addActionListener() methods adds a listener to your buttons,so that they can respond to events occurred on them. The method requires an actual listener as an argument to attach it with the button. this actual listener is any class that implements ActionListener. In your case this listener class is your applet main class itself. Therefore we use an "this" as the parameter. "this" denotes the present class.
Hope u understood.

thank u very much .
U hav made it very clear.
But one last thing
b.addActionListener(new actionapplet())
this also uses an instance of the applet class which is implementing actionListener
Why are we not using this in case of applets
EVen if we use this the code does compile but applet is not getting properly initialised.
So here the code compiles properly which means that its not an error to register in such a fashion.
Then why am i unable to get a properly initialised applet??

b.addActionListener(new actionapplet())

How will the listener code in the NEW instance of the actionapplet class communicate with the currently running one? The new object will be called when an Action Event occurs but what can it do? It is isolated from the variables and methods in the first actionapplet.

see, when u give the parameters as new actionapplet(),this creates a new instance of actionapplet and registers it as the listeners. now this is syntactically and semantically right because the addListener requires an listener as its parameter. So this cause no trouble in compiling.
But this is logically incorrect because when u use the new instance as the actionlistener the actionPerformed method of that instance is called and thus the paint method of that instance is called not the current applet's (this) paint method.
so u get to see no change in your applet window.

thank u so much.
So in case of applets ,
is using 'this' is the only way of pasing argument?

in case of applets

It has nothing to do with the code being an applet.
The way to use Action Listeners is the same for any class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.