Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

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

SGLayout, a GridLayout rewrite, improves your layout control

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
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):

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

commentBy Anonymous on July 8, 2009, 6:02 amnice information i realy like u r approach but u may also work hard . regaerd techplazza.com

Reply | Read entire comment

Links not reachableBy Anonymous on March 3, 2009, 10:46 amSome of the links in your Resources below this one are not linked.

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources