This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I am writing a java application and trying to implement event handling. I'm trying to implement a WindowsAdapter and a ActionListener. Since I'm already extending JFrame for my Java app, I can't extend the WindowsAdapter. So I decided to create inner classes for the WindowsAdapter and the ActionListener. My question is: is it possible to have two inner classes within an eclosing class ? If not how do I get around my problem of trying to implement WindowsAdapter and ActionListener ? Thanks
Are you sure about that Mike ? I tried extending the the WindowAdapter from the inner class, but then the ActionListener method does not seem to be recognized. When I try to compile my code, its states the actionPerformed method cannot be found even though its within the inner class. Are you sure you can extend a class from an inner class ? Thanks
Did you declare that the inner class implements ActionListener (as well as extending WindowAdapter)? It doesn't matter whether the class is an inner class or outer - it can extend one other class, and implement as many interfaces as you want.
There are two ways, and I'll give you both:(the ....'s imply "more code here"). That's the anonymous inner class way of doing it. This is useful when there is only one time that you would add the listener (which is usually the case). If you had a bunch of 'things' that you need to attach the listener to, then you might declare it as a 'named' (non-anonymous) inner class, and add it a bunch of times. Something like: Here we have the inner class extending the convenience class, and the outer class uses it just once. It's hard to imagine using it more than once for this *particular* example, but the point is, you could.
hope that helps you out. [ July 16, 2002: Message edited by: Mike Curwen ]