• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why implement actionlistener as an inner class

 
C Kutler
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to learn about implementing JavaHelp in a program by reading the JavaHelp demo programs.

I do not understand why they are doing something in particular. I am not experienced with listeners or inner classes and hope it is apparent to someone who is, why they implemented it this way.

Here is one part of the code that I understand:




Now here is the part I don't understand. Note that 1. Why create the inner class? 2. In either example, is there a way to do this such that you don't have to make the two TextField variables be class fields?



 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing forum.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by C Kutler:
1. Why create the inner class?


Because that's a convenient (though possibly questionable) way to make the textfields accessible.


2. In either example, is there a way to do this such that you don't have to make the two TextField variables be class fields?


Yes - pass them to the listener via its constructor. Then you could also make the ActionListener a top level class.
 
C Kutler
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ilja

Another question. You say
Because that's a convenient (though possibly questionable) way to make the textfields accessible.


Am I understanding you correctly that by "make the textfields accessible" you mean making them accessible to other classes. If not, can you clarify.

Also, you say it is "possibly questionable". What would you recommend instead.

It is hard when you work with someone else's code and don't know what was in their mind when the wrote the code. Also, if you are a beginner, it is hard to determine if something looks squirrley because the original author did not know how to do it right or if you (or in my case me :roll: ) think it is squirrley because you don't know enough to understand.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by C Kutler:
Am I understanding you correctly that by "make the textfields accessible" you mean making them accessible to other classes. If not, can you clarify.


I meant "make it accessible" to the action.


Also, you say it is "possibly questionable". What would you recommend instead.


With an Action implementation that is more than a few lines, I would probably make it a top level class and pass the required objects (like the textfields) via the constructor.

It is hard when you work with someone else's code and don't know what was in their mind when the wrote the code. Also, if you are a beginner, it is hard to determine if something looks squirrley because the original author did not know how to do it right or if you (or in my case me :roll: ) think it is squirrley because you don't know enough to understand.


I can sympathize with that feeling...
 
Jez Nicholson
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An auxiliary question is: should you put the code that does the actual work into a method and have the listeners call that? After all, the listener is about wiring buttons to things that the class can do.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeremy Nicholson:
An auxiliary question is: should you put the code that does the actual work into a method and have the listeners call that? After all, the listener is about wiring buttons to things that the class can do.


I use both techniques - mostly dependend on how reusable or tightly coupled to the outer class the functionality is. Sometimes it also makes sense to split it between both classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!