I've been a little neglectful keeping on top of Java 6, but my recent trip to the Java Tutorial, which has in my experience been one of the best "how to" programming sites on the net, shows me that the marketing guys have infiltrated this once idealistically perfect site.

Here's what I saw.

How to Use Text Areas (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components):

The TextAreaDemo example introduces an editable text area with a special feature — a word completion function. As the user types in words, the program suggests hints to complete the word whenever the program's vocabulary contains a word that starts with what has been typed.


That's exactly what I needed. I'd been looking at some solutions with more functionality, where you start typing a word and a number of possibilities appears in some sort of select-list below, including the Netbeans editor and a few examples from weblogs. Each was more overhead than I wanted, and just short of writing some simplistic autocomplete code, I stumbled over this.

So where's the marketing? How about the stuff that's Java 6 only, like the incredible overkill of the Java 6 only GroupLayout? Sure, you can add GroupLayout to your 1.5 and possibly 1.4 application, but why do I need this for a form with a single label and single text area?

Let's look at the code. Here's the layout jive necessary with GroupLayout.

GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);

//Create a parallel group for the horizontal axis
ParallelGroup hGroup =
layout.createParallelGroup(GroupLayout.Alignment.LEADING);
//Create a sequential and a parallel groups
SequentialGroup h1 = layout.createSequentialGroup();
ParallelGroup h2 =
layout.createParallelGroup(GroupLayout.Alignment.TRAILING);
//Add a scroll panel and a label to the parallel group h2
h2.addComponent(jScrollPane1,
GroupLayout.Alignment.LEADING,
GroupLayout.DEFAULT_SIZE, 212,
Short.MAX_VALUE);
h2.addComponent(jLabel1,
GroupLayout.Alignment.LEADING,
GroupLayout.DEFAULT_SIZE, 212,
Short.MAX_VALUE);

//Add a container gap to the sequential group h1
h1.addContainerGap();
// Add the group h2 to the group h1
h1.addGroup(h2);
h1.addContainerGap();
//Add the group h1 to hGroup
hGroup.addGroup(Alignment.TRAILING,h1);
//Create the horizontal group
layout.setHorizontalGroup(hGroup);

//Create a parallel group for the vertical axis
ParallelGroup vGroup =
layout.createParallelGroup(GroupLayout.Alignment.LEADING);
//Create a sequential group
SequentialGroup v1 = layout.createSequentialGroup();
//Add a container gap to the sequential group v1
v1.addContainerGap();
//Add a label to the sequential group v1
v1.addComponent(jLabel1);
v1.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
//Add scroll panel to the sequential group v1
v1.addComponent(jScrollPane1,
GroupLayout.DEFAULT_SIZE, 100,
Short.MAX_VALUE);
v1.addContainerGap();
//Add the group v1 to vGroup
vGroup.addGroup(v1);
//Create the vertical group
layout.setVerticalGroup(vGroup);
pack();


Now here's what I need to get the same form, give or take, with layout code that'll work with every version of Java since Swing was released as an add-on jar. (Why bother? Mac OS X 10.4 has Java 1.5, not 6, and no GroupLayout by default. Sounds like a decent reason to me.)

BorderLayout layout = new BorderLayout();
getContentPane().setLayout(layout);

this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
this.getContentPane().add(jLabel1, BorderLayout.NORTH);
this.pack();


That's right, the code becomes more readable and compatible. Mr Pibb + Red Vines = Crazy Delicious!

Maybe I'm an old-school idealist, but I don't believe in including a single extra line of code, much less a major code concept, in a tutorial than is absolutely necessary. Tutorial code should be designed to be written by hand, and should encourage its users to make small changes to see how those changes influence what happens. With GroupLayout, it's very difficult to add another few GUI widgets, difficult enough that the Tutorial itself tells us so on its page regarding how to use GroupLayout. Don't miss the Netbeans pimp in the following quote.

Note: This lesson covers writing layout code by hand, which can be challenging. If you are not interested in learning all the details of layout management, you might prefer to use the GroupLayout layout manager combined with a builder tool to lay out your GUI. One such builder tool is the NetBeans IDE. Otherwise, if you want to code by hand and do not want to use GroupLayout, then GridBagLayout is recommended as the next most flexible and powerful layout manager.


That last sentence is particularly bad marketing speak. See how they ignore the fact that saying "if you want to code by hand" is a superset of "do not want to use GroupLayout," the latter left in solely to deaden the [admittedly small] shock of what they just told you -- GroupLayout is for IDEs, not text editors. And the pseudo-objective tone of "One such builder tool is the NetBeans IDE," is borderline obnoxious.

For older school programmers, that paragraph would read, "GroupLayout was created to help support GUI RAD in Netbeans, and works best in concert with that IDE. GridBagLayout is the most flexible and powerful layout manager created with hand-coding GUIs in mind." Alternately, for even older school folk, the text would be, "GroupLayout is not for hand-coded GUIs. Use GridBagLayout or BorderLayout instead." And a comment in the TextAreaDemo example saying, "This code intended for use in Netbeans," or something similar explaining why the horribly unwieldy layout manager was used, if it has to be there, would have been nice.

Tutorials should present simplest case information that allow an informed programmer to learn a new trick in a simplest case, clean room environment, and then have the tools to figure out how to best integrate that into his or her work flow and environment. Pimping a specific Java IDE has absolutely nothing -- no, I hear your argument, and trust me. They have nothing to do with one another -- to do with learning to use JTextArea. Learning how to introduce JTextAreas using Netbeans is Netbeans' business. Doing it in Eclipse is Eclipse's business, etc.

I don't care to see the IDE wars inserted into my tutorials. It's painful to see that Sun now prefers the opposite.

Labels: , ,