Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
BorderLayout and FlowLayout generally prove useful, but developers often start running into difficulties when coding more complex layouts. GridBagLayout, in particular, is very powerful, but many developers find it difficult and nonintuitive. Personally, I follow the "least
surprise" principle when I look at layout managers: If you are surprised at a layout manager's behavior, it probably was designed
poorly. On this basis, GridBagLayout gets particularly poor marks. With that in mind, a better way to get similar results must exist.In "Java Tip 121: Flex Your Grid Layout" (JavaWorld, December 2001), Bogdan Dorohonceanu describes a GridLayout subclass that avoids the requirement that a grid's elements possess the same dimensions. In this Java Tip, I describe a layout manager, SGLayout, with similar flexibility and direct control over margins, gaps between rows and columns, and fine control over how to locate
a component within the grids.
In one example, a simple password panel, Dorohonceanu incorporates a series of labeled text fields into a grid and shows how to improve the panel's appearance by decreasing the label column's width relative to that of the text field column.
Thinking I could improve upon GridLayout, I wrote SGLayout as a complete replacement. In so doing, I incorporated much of GridBagLayout's alignment power.
Note: You can download this article's complete source code from Resources.
I commence discussion with a rather silly GUI frame, seen in Figure 1, that illustrates SGLayout's possibilities. (I implemented this demo as SGDemo.java.) A JPanel instance, to which an SGLayout manager has been set, replaces the JFrame's contentPane:
JPanel contentPanel = new JPanel();
SGLayout contentLayout = new SGLayout(3, 2, , SGLayout.FILL,
SGLayout.FILL, 15, 5);
contentPanel.setLayout(contentLayout);

Figure 1. Grid cells and alignments using SGLayout
The layout manager in this case defines management of three rows and two columns. The component added to each grid position will fill the position in both the X- and Y-directions, and there will be a 15-pixel gap between the columns and 5-pixel gaps between the rows. It's complicated but straightforward. (Note that the conventional schizophrenia applies: rows before columns, but X-axis before Y-axis.)
A series of statements then destroys that arrangement's simple order:
layout.setRowScale(1, 2.0); layout.setColumnScale(0, 0.65);
These statements make row 1 wider (by a factor of 2.0 relative to rows 0 and 2, which have a 1.0 default scale-value) and column 0 narrower than column 1 (by a factor of 0.65). The scale values are all relative and are maintained on resizing of the container:
layout.setColumnAlignment(0, SGLayout.RIGHT, SGLayout.TOP);
This statement changes the alignment of all grid cells in the 0th column. Next, you change the individual cells' alignment (on a row, column basis):
SGLayout and friendsFree Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq