• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ActionListener Interface?

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation buddy....i got it now..
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok...thanks for the information dude
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic