Skip to main content

Avoiding the GridBag

Posted by daniel on July 22, 2003 at 6:25 AM PDT

The GridBagLayout is kind of the brocoli of the Swing layout managers. It's good for you, there are people who seem to really like it, but it's brocoli. In Java Today we feature a project named packer. In many ways the new approach isn't much more accessible to new users but the code that a complex layout would require is greatly reduced.

Greg offers the following example of using GridBagLayout and GrigBagConstraint to layout a labeled text field that expands horizontally.

GridBagConstraints gc = new GridBagConstraints();
GridBagLayout gl = new new GridBagLayout();
Panel p = new Panel();
p.setLayout( gl );

Label l = new Label("Username:");

gc.fill = GridBagConstraints.HORIZONTAL;
gc.weightx = 0;
gc.gridx = 0;

TextField f = new TextField();
gc.weightx = 1;
gc.gridx = 1;

Using his Packer class, the code can be simplified as follows.

 Panel p = new Panel();
Packer pk = new Packer(p);
Label l = new Label("Username:");
TextField f = new TextField();
pk.pack( l ).fillx().weightx(0).gridx(0);
pk.pack( f ).fillx().gridx(1);

You certainly use less code. Is it more readable or easier to use? Join the packer project and provide your feedback.

In Weblogs, Erb Cooper's debut entry has the provocative title Java 1.5: The end of Java?. Erb has learned to love the changes and additions to the Java language (such as anonymous inner classes) in the past. He's not looking forward to the changes coming in 1.5. His entry explains that the implementation of generics and the overloading of for feel very C-like to him and he worries what it will mean in this time that developers are weighing the use of Java against C#.

Marc Hadley's weblog entry W3C Announces Workshop on Binary Interchange of XML Information Item Sets points to what is currently a passworded link. Hadley details the fundamental issue that XML files are easier to read than binary but they are bulky. Hadley hopes that a binary representation of XML might help improve the performance of web services.

In the Also Today section, in addition to the packer project, we link to the JavaWorld article Add concurrent processing with message-driven beans. Amit Poddar shows how different models of concurrency can be applied to a J2EE application.

Steve Mallett, the Java Today news editor has gathered the following
Java Today News Headlines : href=""> "Eclipse Stable Build: 3.0M2 Released", "Base Beans Announces basicPortal v 0.9.8", "LGPL reciprocity terms still unclear OSL and AFL licences offer closure", "Compuware adds Java modeling", "IceMail 4.0.2 Released", and "Book Review of Eclipse in Action".

Once this page is no longer featured as the front page of href=""> Java Today it will be archived at href=""> You can
access other past issues by changing the address appropriately.