This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes The best approach for event listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "The best approach for event listener" Watch "The best approach for event listener" New topic
Author

The best approach for event listener

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Hi ranchers.
in swing application, we have the "event listeners".
we developing my application , I used :
-------------------------------------
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// some code
}
});
-------------------------------------
but borland used the following approach in thier JBuilder :
myButton.addActionListener(new Manager_myButton_actionAdapter(this));
then :
void myButton_actionPerformed(ActionEvent e) {
// some code here
}
then , in the same java file, they create a new class :
class Manager_myButton_actionAdapter implements ActionListener {
Manager adaptee;
Manager_myButton_actionAdapter(Manager adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.myButton_actionPerformed(e);
}
}
so is the best approach ? and what is the approach that is used in the real applications ?
(Borland is a big company and has a long history with IDEs , so should I used their approach?)
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

It depends on what you are trying to do, but personally, the first version is much more readable, and should be OK to use in the majority of circumstances. Probably the *worst* designed GUIs I have seen have been from the code created through visual GUI creators, so I would definately not try to learn good coding habits from code generation tools. This design just probably makes it easier for the GUI design tool to keep track of what listeners go with what buttons.

The only times you might want to not use the anonymous ActionListener defined in place would be if you need to reuse the ActionListener somewhere else. It's also possible to run into performance problems by defining so many small classes, but I've really never seen this to be an issue in any of the code I've worked on... you'd probably have to have millions of anonymous ActionListners for this to be a real problem.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
Don't get me started about those stupid light bulbs.
 
subject: The best approach for event listener
 
Similar Threads
Javacap mock test que-27
Strange problem, closing dialogs
actionPerformed Best Practices
what features of java need further improve ?
Anonymous Inner class extends/implements