Title: Professional Java Programming
ISBN: 186100382x
US Price: $ 59.99
Canadian Price:
C$ 89.95
UK Price: £ 45.99
© Wrox Press Limited, US and UK.

Reviews : Java Books :
Professional Java Programming : Using Layout Managers

Container Size

When calculating the preferred and minimum size values for a container, FlowLayout can't make any assumptions about the width of the container or about how many rows of components should be created. Instead, the size values are calculated so that the container will be wide enough to contain all child components in a single row. For example, the preferred width value returned by a FlowLayout is determined by adding three values:

  • The left and right inset values of the container
  • The amount of space needed to provide horizontal gaps
  • The sum of all child components' preferred widths

In other words, a FlowLayout's preferred width is the amount of horizontal space needed to display all its child components from end-to- end on a single row using their preferred sizes.

To determine the container's preferred height, FlowLayout first identifies the preferred height of the tallest component in the container. The container's preferred height is then calculated as the sum of largest component height, the number of pixels needed to provide vertical gaps at the top and bottom edges of the container, and the container's top and bottom inset values.

The value returned for a container's minimum size by a FlowLayout is calculated in essentially the same way as the preferred size, but is done using the minimum sizes of the components in the container instead of their preferred sizes.

GridLayout

This layout manager divides the available space into a grid of cells, evenly allocating the space among all the cells in the grid and placing one component in each cell. For example, in the following code, four buttons are added to a container that uses a GridLayout:

import java.awt.*;
import javax.swing.*;

public class GridSizeTest extends JFrame {

  public static void main(String[] args) {
    GridSizeTest gst = new GridSizeTest();
      gst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      gst.pack(); // pack() makes the window the right
      gst.setVisible(true); // size for all it's components to fit
  }

  public GridSizeTest() {

    Container pane = getContentPane();
    pane.setLayout(new GridLayout(2, 2));
    JButton button = new JButton("First");
    pane.add(button);
    button = new JButton("Second with a very long name");
    pane.add(button);
    button = new JButton("Hi");
    button.setFont(new Font("Courier", Font.PLAIN, 36));
    pane.add(button);
    button = new JButton("There");
    pane.add(button);
  }

}

When this code is compiled and executed, it produces a display like the one shown below. Notice that all of the buttons are allocated the same amount of space, even though one button's label is wider than the others and another has a label that's much taller than the rest:

As this example illustrates, GridLayout is useful when some rectangular portion of your interface contains adjacent components that should all be assigned the same size and when the amount of space between those components is consistent. For instance, you might use a GridLayout to create a panel that contains a row of buttons that are all the same size and which have the same amount of space between one another.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.