"Il y a peu de choses qui me soient impossibles..."
Steve
Steve Luke wrote:A better approach, in my opinion is a bit of a combination:
The Eavesdropper then is not tied to using an actionListener, no one can trigger its doEavesdropping method without going through whatever method you define (Action, or Button). And it can contain several (hopefully related) listeners as well. Note you would pass in both the button and the target in this case.
"Il y a peu de choses qui me soient impossibles..."
Stevens Miller wrote: The anonymous inner class has the advantage of direct access to all of the enclosing object's fields and methods. But it requires a method-within-a-method approach (with the actual event handler being called from inside the anonymous inner class's actionPerformed method), and (I am noticing) tends (in my own case, anyway) to lead to large class files with lots and lots of event handlers (one for every menu item, one for every button, and one for everything any other Swing control might generate as an event I need to handle).
Steve Myers wrote:Also the anonymous class way of implementing the listener does not necessarily require a method-within-method approach, the method could be defined entirely within the anonymous class if you were hand coding the GUI - I have never used NetBeans though.
I think both of these approaches are considered superior to the way Oracle does it in many of their tutorials (ie- public class Demo extends JPanel implements ActionListener).
"Il y a peu de choses qui me soient impossibles..."
Campbell Ritchie wrote:If the serach thing is working, you find there are several ways to write Listeners. There are classes which extend AbstractAction, too, which is nice. If our search facility is working, you will find this sort of post, this, and this.
You may get the impression I think addActionListener(this) is bad programming. If you get that impression, it is well‑founded
If you follow links in those posts, you will find other ways to write Listeners. But they are all on the GUI forum, so I shall send this discussion to GUIs to join them.
"Il y a peu de choses qui me soient impossibles..."
I suppose one advantage of named external classes is that, if you later do want a listener to be able to handle events from more than one source, you already have that listener defined as a class. Is that insufficient reason to put all of my listeners into class files?
Stevens Miller wrote:
Steve Myers wrote:
I think both of these approaches are considered superior to the way Oracle does it in many of their tutorials (ie- public class Demo extends JPanel implements ActionListener).
Please help me be sure I understand what you're saying. Are you saying it is considered superior to the addActionListener(this) approach, or to the entire idea of of using an external object as your listener (as opposed to an inner class object)?
Steve Myers wrote:The one form (used in some Oracle tutorials) you want to avoid is something like
"Il y a peu de choses qui me soient impossibles..."
"Il y a peu de choses qui me soient impossibles..."
Stevens Miller wrote:
Steve Myers wrote:Also the anonymous class way of implementing the listener does not necessarily require a method-within-method approach, the method could be defined entirely within the anonymous class if you were hand coding the GUI - I have never used NetBeans though.
Yes, that's a good point too. No problems if each anonymous inner class has its own actionPerformed method.
Stevens Miller wrote:
Steve
Steve Luke wrote:One of the reasons to use the external class is to separate the GUI code (your JFrame) from your business logic and control (the work that needs to be done, and marshaling commands to the place where the work should be done). The doFileOpen() and doFileClose() methods should probably be moved out of your GUI class unless the methods are trivial (and don't get used to putting trivial methods into the GUI code either - because they soon become non-trivial). And, if you do put the methods in the GUI code then having the FileListener as a separate class is an extra layer of indirection that doesn't really add anything (because the working code is still in the GUI class). It is an increase in complication for no benefit, so I would suggest axing it.
"Il y a peu de choses qui me soient impossibles..."