Java Basics
Why aren't Java GUIs easier?
I don't want to exaggerate the difficulties of working with Java GUIs because they are much easier to build than Microsoft Windows interfaces in C++. But they are harder than those in Visual Basic, for example.
Flexibility in the big Java problem for beginners (and advantage to advanced programmers) -- there are a huge number of GUI classes, a huge number of methods that can be used in these classes, and many ways to structure the framework.
Too many classes. There are lots of GUI classes However, many of these are useful only in special cases, or should never be used. It's important to give perspective on what's important. Some authors, eg, Deitel and Deitel, present everything they've learned, regardless of how trivial it is. In fact, they sometimes devote the most text to things they find especially interesting, regardless of their utility.
For the most part only the most essential classes will be explained.
Too many methods. There are over 400 methods
in the JTextField
class (mostly inherited of course)!
The standard Java documentation doesn't tell you which of these
are useful. In fact, you will write most programs using
only TWO JTextField methods.
These notes will try to concentrate on those methods needed to know to write the vast majority of plain vanilla applications.
Too many ways to put it all together. Even after you know which elements you want to work with, and the few methods that are really useful, you will find out that there are a number of ways to connect them. Again the curse of flexibility gets in the way of providing a simple answer. For each reasonable way to connect the parts, there are many that don't scale as the problem becomes larger, and will lead you down a path to eventual disaster.
The approach here is concentrate on some very common, and good, ways of organize the code -- subclass JFrame, use named inner class listeners, separate the GUI and model, .... There are good alternatives to what is presented here, and I'll try to mention them in these commentaries as time permits.
Future directions - Sun's Application Framework?
The is no established best framework for building a GUI application. This has been a giant problem, but there is a chance it will be solved, or at least partially, when Sun releases it's Swing Application Framework proposal.
See Hans Muller's blog Swing Application Framework JSR-296 presentation, which outlines preliminary thinking on the matter.
Julian Doherty points has a nice overview of Swing problems in in his JRuby can save Swing - Follow up> blog entry. Although it is nominally about JRuby and Groovy and their relationship to Swing, it really is about the bigger problems.