File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you have 2 inner classes ?

 
Jerry McClain
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chad McGowan
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have multiple inner classes. It is pretty common to make your action listeners anonymous inner classes. Each component on your frame would then have its own inner class.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can have two inner classes, but unless I'm off the mark, this hint might turn a light bulb on:

An inner class can extend other classes (namely, WindowAdapter)
 
Jerry McClain
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jerry, yup I'm sure.

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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic