Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ActionListener Interface?  RSS feed

 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the java doc of ActionListener Interface it says:

The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.

In this the object refer to the class who implements the ActionListener interface..then why is that we can't pass the object name in the addActionListener method...why do we have to pass "this" as an argument....

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vasiq Molvizadah wrote:
In this the object refer to the class who implements the ActionListener interface..then why is that we can't pass the object name in the addActionListener method...why do we have to pass "this" as an argument....


I think you are talking about typical examples which have a class implement the ActionListener and in the same class you register the listener. In such scenarios, this refers to the implementing class itself.
However, that is not the only way how you could do it.
You can have another class implement the interface and use an instance of that class to register your listener


or you can also do it, using anonymous inner classes, like

or even using the constructor like
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation buddy....i got it now..
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.

On a side note, having a class implement the listener is not that a good idea. The code in the books is for illustration purposes only. If you have multiple event sources and a common listener class, you end up with multiple if else statements which can get messy.

In real life, you can follow a simple rule of the thumb.
If you got one source with an unique action, use anonymous inner classes.
If you got multiple sources with the same action, subclass Action/AbstractAction and share that instance with the sources.

This way, your code will be easy to understand and easy to maintain.
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the first point..but really didn't understand the second one...could you please give me an example on the second point....

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider an application where the user can either choose to shut down using the File->Exit menu or by clicking on the "Exit" button.
So you got two sources, with the same action.

Step 1: Subclass AbstractAction


Then in your code, you share the same exit instance.
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But we can also do this with the help of ActionListener Interface...what's so special in extending AbstractAction class....
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do not unnecessarily quote the whole posts. It makes the page longer and difficult to read. I have edited the quotes from your post. While replying, you can use the reply button.

Action/AbstractAction provides you built in facilities to
1) Set mnemonics
2) Set accelerators
3) Enable/Disable
4) Tooltips

All of these are missing if you use ActionListener.
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...thanks for the information dude
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!