A perfect example of why Swing needs to start anew.
Posted on Saturday, March 14, 2009 at 4:09 PM.Today I read an article that does a good job highlighting the nature of the problems with Swing. Although it doesn't mention which version of Java was analyzed, it does point out that at least one version of Swing's JMenu class has 433 methods. Although many of these are inherited from its six parent classes, it still provides a good example of how unwieldy Swing has become.
Part of the problem clearly arises from how Swing is based directly on AWT. Obvious overlap is visible between the java.awt.Component and javax.swing.JComponent classes. JComponent ends up overriding many of the methods of Component. Some may argue that this is an example of good code reuse. To me, it suggests a broken inheritance hierarchy.
Unfortunately, the decision to base Swing directly on top of AWT makes the removal of AWT difficult, if not outright impossible. In the past I have suggested that a rewrite of Swing is the only viable way of dealing with these problems. It becomes extremely difficult to effectively clean up a class hierarchy, while still maintaining backwards compatibility. Thus the only option is to, unfortunately, start from scratch.
There would, of course, be other benefits to eliminating such obtuse classes and class hierarchies. There's the possibility of a reduced on-disk footprint for the .class (and .jar) files. Fewer resources, both in terms of memory and processing, would be needed to load and compile the Java bytecode. The burden on the auto-complete functionality of IDEs like Eclipse and NetBeans would be reduced, allowing them to be more responsive and less memory-intensive. A cleaner API would no doubt be helpful to developers.
Although many OO advocates suggest that object-oriented techniques allow for more complex software systems to be constructed, I think we can clearly see that that's not necessarily a good thing. Inheritance, for the purpose of code reuse, can lead to absurdities like that of the Swing JMenu class. Although complete rewrites are generally frowned upon and can often turn out poorly, we do get to the point where we don't have much of a choice. And after 15 years, we're seeing many of Java's core technologies, including AWT and Swing, getting awfully close to that point.








