Reputation: 419
yellowButton.addActionListener(this);
what is (this) referring to in the addActionListener method. and can someone tell me how actionListener method works?
Upvotes: 0
Views: 163
Reputation: 25249
this
refers to the current object. The current object implements the interface ActionListener. When an action happens, the actionPerformed method is invoked.
Upvotes: 2
Reputation: 597106
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.
Upvotes: 3