Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Are Listener Interfaces redundant?  RSS feed

 
Siobhan Murphy
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would a programmer bother to implement a Low Level Event Listener Interface, when he or she can extend an Adapter class (which is much simpler).
In other words: are Listener Interfaces redundant?
Siobhan
 
John Zukowski
Author
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The adapters are only there if the listener interface has more then one method, and in most cases the Swing-specific event listeners don't have adapters.
You can only extend from one class, so if you subclass an adapter you've thrown away that one chance to subclass something more meaningful.
You can only extend from one class, so if you want a class to implement multiple interfaces, you're out of luck if you only consider adapters.
------------------
John Zukowski Author of "Definitive Guide to Swing for Java 2" and "Java Collections"
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. For instance, I created a formulaBar class which monitors changes in a JTable. It extends JTextField, but implements every table listener, and a few new ones we created.
That said, if you're just adding an anonymous inner class to listen, you should use the adapter class if one exists. HTH.
 
Siobhan Murphy
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John and Eric.
Your answer is a help.
Siobh�n
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!