• Post Reply Bookmark Topic Watch Topic
  • New Topic

"this" as an ActionListener method argument  RSS feed

 
U. Remmarot
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fellow programmers.

I am currently working with the book "Head First Java".
There is some code to explain ActionListeners that I don't get. I typed it:



line 19 is what I don't understand. What does the keyword "this" mean. What does it refer to?
I only know from the API that it is supposed to be an ActionListener.

Thanks in advance for your help.
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Ah the "this" keyword, which generally means the current object you are working on. Java tutorial on this keyword

In your action listener example, note that how the class is declared:


That line is really saying the SimpleGui1B class "is an" ActionListener.

So in line 19, the button is adding itself as the action listener.
 
U. Remmarot
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
I've already took a look into that tutorial. But it doesn't seem to cover this case.
If I got it right "this" is supposed to refer to the class. But no matter what I pass as an argument here, nothing seems to substitute "this".

So, I still don't get the argument. If you say the button adds itself as an ActionListener, does "this" refer to the button reference variable? It won't work either if I try to pass it.
 
K. Tsang
Bartender
Posts: 3633
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The button is adding a action listener. Suppose the SimpleGui1B class is just a regular class.



Now do you see how the Head First code and my example differ? The "this" refers to MyActionListener in my code. If you were to put the actionPerformed method in SimpleGui1B class you would get Head First code.
 
U. Remmarot
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks at least for showing me a different way to write it!
I need to think about that a little.
 
Stephan van Hulst
Saloon Keeper
Posts: 7001
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something else: Never write code like this yourself. This is an incredibly persistent and obnoxious pattern. GUI components shouldn't listen to actions they trigger themselves. It's unclear, and it violates all sorts of good coding guidelines.

This is how you should add listeners (starting with Java 8):

For Java 7 and older, do it the way K. Tsang showed you.
 
U. Remmarot
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan,

thanks for the reply.
What do you mean by code like this and what pattern are you referring to?
Which guidelines does it violate?
I just copied the code from the book I'm reading.

Do you have a source for your example? Why is it that with Java 8 you write it like this? It isn't obvious from my book.



Stephan van Hulst wrote:Something else: Never write code like this yourself. This is an incredibly persistent and obnoxious pattern. GUI components shouldn't listen to actions they trigger themselves. It's unclear, and it violates all sorts of good coding guidelines.

This is how you should add listeners (starting with Java 8):

For Java 7 and older, do it the way K. Tsang showed you.


 
Stephan van Hulst
Saloon Keeper
Posts: 7001
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm referring to when you write a class that represents a part of your view also implements a listener. Sadly, a lot of books contain code that do this. It violates the principle of single responsibility. A view component should only be concerned with presenting data to the user. A listener should only know what to do when a certain event occurs. Now, it looks like you can attach an entire view to a button, which creates a lot of confusion as you've already noticed with the usage of the keyword 'this'.

A GUI is a GUI. A listener is a listener. They should not be the same class.

As for the Java 8 example: It's a concise and clear way of associating behavior with a control. It's easy to see what it does, and it minimizes visual clutter. It's not that you should *always* do this, but for simple GUIs that don't require complex actions, it's the most readable way. Sorry, I don't have a source for you, this is my personal experience.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Something else: Never write code like this yourself. This is an incredibly persistent and obnoxious pattern. . . .
Surely you would call that an anti‑pattern.

Whenever I see this for any kind of Listener I start seeing that anti‑pattern. For listeners of all kinds you should create a class (or use a λ for functional interfaces as in your example). There is maybe one exception. When you have a listener which listens to action involving the component. For example a mouse listener where you want the location within a particular component.
 
Stephan van Hulst
Saloon Keeper
Posts: 7001
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really understand why that would be an exception? Either use an internal class or lambda that has access to the field referencing the component, or pass the component to the constructor of the listener. Can you illustrate the exception?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it might not be a good exampleThat is not a brilliant example and it is not something you will want often.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to our GUIs forum because we usually discuss Listeners there.
 
Stephan van Hulst
Saloon Keeper
Posts: 7001
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That is not a brilliant example and it is not something you will want often.



Why would you ever want it? The listener doesn't even use the component it listens to in this case. And if the XYZPanel uses it to listen to other components, then why not just do the following?
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
A GUI is a GUI. A listener is a listener. They should not be the same class.


But is the class at the top actually a GUI, or is it simply building the GUI?

It's not extending (thankfully) any GUI classes.

(More for the sake of discussion than anything else, as I wouldn't write it that way either).
 
Stephan van Hulst
Saloon Keeper
Posts: 7001
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If my class aggregates visual components and exposes methods to access these, I consider it a GUI, so this includes classes that don't extend JFrame or JPanel, but initialize fields that contain references to them.
 
Dave Tolls
Ranch Hand
Posts: 2504
27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:If my class aggregates visual components and exposes methods to access these, I consider it a GUI, so this includes classes that don't extend JFrame or JPanel, but initialize fields that contain references to them.


That's OK, then.
I was just checking as the later examples all extend components...
 
U. Remmarot
Greenhorn
Posts: 7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it okay to use an inner class as an ActionListener?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Then you can have one listener object for each action required.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!