The sun.awt Layout Collection

The sun.awt package defines four additional layouts. The first two, HorizBagLayout and VerticalBagLayout, are available only when used with Sun's JDK or Internet Explorer, since they are not provided with Netscape Navigator and may not be available from other vendors. Therefore, these layout managers should be used selectively within applets. The third layout manager, VariableGridLayout, is available with Netscape Navigator 2.0 or 3.0 and Internet Explorer. Usage of this layout manager is safer within applets but is still at your own risk. The final layout manager is introduced in Java 1.1, OrientableFlowLayout. Only time will tell where that one will be available. Any of these layout managers could be moved into a future version of java.awt if there is enough interest.

HorizBagLayout

In a HorizBagLayout, the components are all arranged in a single row, from left to right. The height of each component is the height of the container; the width of each component is its preferred width. Figure 7.16 shows HorizBagLayout in use.

Figure 7.16: HorizBagLayout

[Graphic: Figure 7-16]Constructors

LayoutManager methods Miscellaneous methods
sun.awt.HorizBagLayout[hgap=0] 

VerticalBagLayout

The VerticalBagLayout places all the components in a single column. The width of each component is the width of the container; each component is given its preferred height. Figure 7.17 shows VerticalBagLayout in use.

Figure 7.17: VerticalBagLayout

[Graphic: Figure 7-17]Constructors

LayoutManager methods Miscellaneous methods
sun.awt.VerticalBagLayout[vgap=0] 

VariableGridLayout

The VariableGridLayout builds upon the GridLayout. It arranges components on a grid of rows and columns. However, instead of giving all components the same size, the VariableGridLayout allows you to size rows and columns fractionally. Another difference between VariableGridLayout and GridBagLayout is that a VariableGridLayout has a fixed size. If you ask for a 3x3 grid, you will get exactly that. The layout manager throws the ArrayIndexOutOfBoundsException run-time exception if you try to add too many components.

Figure 7.18 shows a VariableGridLayout in which row one takes up 50 percent of the screen, and rows two and three take up 25 percent of the screen each. Column one takes up 50 percent of the screen; columns two and three take 25 percent each.

Figure 7.18: VariableGridLayout in Netscape Navigator

[Graphic: Figure 7-18]

Here is the code that creates Figure 7.18:

import java.awt.*; java.applet.Applet; import sun.awt.VariableGridLayout;
public class vargrid extends Applet {
 public void init () {
 VariableGridLayout vgl; setLayout (vgl = new VariableGridLayout (3,3)); vgl.setRowFraction (0, 1.0/2.0); vgl.setRowFraction (1, 1.0/4.0); vgl.setRowFraction (2, 1.0/4.0); vgl.setColFraction (0, 1.0/2.0); vgl.setColFraction (1, 1.0/4.0); vgl.setColFraction (2, 1.0/4.0); add (new Button ("One")); add (new Button ("Two")); add (new Button ("Three")); add (new Button ("Four")); add (new Button ("Five")); add (new Button ("Six")); add (new Button ("Seven")); add (new Button ("Eight")); add (new Button ("Nine"));
}
} 
Constructors Support methods

The distinguishing feature of a VariableGridLayout is that you can tell a particular row or column to take up a certain fraction of the display. By default, the horizontal space available is split evenly among the grid's columns; vertical space is split evenly among the rows. This group of methods lets you find out how much space is allotted to each row or column and lets you change that allocation. The sum of the fractional amounts for each direction should add up to one. If greater than one, part of the display will be drawn offscreen. If less than one, additional screen real estate will be unused.

LayoutManager methods

The only method from GridLayout that is overridden is the layoutContainer() method.

Miscellaneous methods
sun.awt.VariableGridLayout[hgap=0,vgap=0,rows=3,cols=3, rowFracs=[3]<0.50><0.25><0.25>,colFracs=[3]<0.50><0.25><0.25>] 

OrientableFlowLayout

The OrientableFlowLayout is available for those who want something like a FlowLayout that lets you arrange components from top to bottom. Figure 7.19 shows OrientableFlowLayout in use.

Figure 7.19: OrientableFlowLayout

[Graphic: Figure 7-19]Constants

Since OrientableFlowLayout subclasses FlowLayout, the FlowLayout constants of LEFT, RIGHT, and CENTER are still available.

Constructors LayoutManager methods Miscellaneous methods
sun.awt.OrientableFlowLayout[orientation=vertical, sun.awt.OrientableFlowLayout[hgap=5,vgap=5,align=center]]