Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Tip 129: SGLayout—a layout manager for the rest of us

SGLayout, a GridLayout rewrite, improves your layout control

  • Print
  • Feedback
A good layout manager proves essential for creating a good graphical user interface (GUI), but many beginner developers find the standard Java layout managers difficult to master. Of these, 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.

SGLayout and its friends

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):

  • Print
  • Feedback

Resources