0
yellowButton.addActionListener(this);

what is (this) referring to in the addActionListener method. and can someone tell me how actionListener method works?

dave
  • 419
  • 1
  • 5
  • 10
  • 1
    Interesting; we just had one of your classmates here a couple of hours ago. http://stackoverflow.com/questions/6310649/can-somebody-explain-how-this-works – Michael Myers Jun 10 '11 at 21:32

2 Answers2

3

this is the current object. It has to implement the ActionListener interface.

This is often used in order to have all the related code in one place. But you can also use yellowButton.addActionListener(new ButtonActionListener()), where ButtonActionListener is another class of yours.

Classes that implement ActionListener should specify what happens when an action happens.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
2

this refers to the current object. The current object implements the interface ActionListener. When an action happens, the actionPerformed method is invoked.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66