I’ve already posted a sample Swing application that used anonymous inner classes to define all of the needed event listeners. This sample is a port of the same functionality, however all of the anonymous inner classes have been replaced with named inner classes.

Named inner classes are defined just like any other class, however they are defined inside of another class (this is compared to anonymous inner classes that are defined at object instantiation time). There are specific rules on where and how they can be accessed outside of the class that are outside of the scope of this post.

One benefit to named inner classes is that they can be instantiated more than once, as compared to the single instantiation of anonymous inner classes. Additionally, as named inner classes function just like other classes, they can implement more than one interface. This code sample demonstrates using one implementation to function as multiple listener types.

The attached code is released under the GNU General Public License.

Click here to download the source.

Comments are closed.