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
Constructors
- public HorizBagLayout ()
- This constructor creates a
HorizBagLayout
with a horizontal gap of zero pixels. The gap is the space between the different components in the horizontal direction. - public HorizBagLayout (int hgap)
- This constructor creates a
HorizBagLayout
using a horizontal gap ofhgap
pixels.
- public void addLayoutComponent (String name, Component component)
- The
addLayoutComponent()
method ofHorizBagLayout
does nothing. - public void removeLayoutComponent (Component component)
- The
removeLayoutComponent()
method ofHorizBagLayout
does nothing. - public Dimension preferredLayoutSize (Container target)
- The
preferredLayoutSize()
method ofHorizBagLayout
sums up the preferred widths of all the components intarget
, along with thehgap
and right and left insets to get the width of thetarget
. The height returned will be the preferred height of the tallest component. - public Dimension minimumLayoutSize (Container target)
- The
minimumLayoutSize()
method ofHorizBagLayout
sums up the minimum widths of all the components intarget
, along with thehgap
and right and left insets to get the width of thetarget
. The height returned will be the minimum height of the tallest component. - public void layoutContainer (Container target)
- The
layoutContainer()
method drawstarget
's components on the screen in one row. The height of each component is the height of the container. Each component's width is its preferred width, if enough space is available.
- public String toString ()
- The
toString()
method ofHorizBagLayout
returns a string with the current horizontal gap setting--for example:
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
Constructors
- public VerticalBagLayout ()
- This constructor creates a
VerticalBagLayout
with a vertical gap of zero pixels. The gap is the space between components in the vertical direction. With a gap of 0, adjacent components will touch each other. - public VerticalBagLayout (int vgap)
- This constructor creates a
VerticalBagLayout
with a vertical gap ofvgap
pixels.
- public void addLayoutComponent (String name, Component component)
- The
addLayoutComponent()
method ofVerticalBagLayout
does nothing. - public void removeLayoutComponent (Component component)
- The
removeLayoutComponent()
method ofVerticalBagLayout
does nothing. - public Dimension preferredLayoutSize (Container target)
- To get the preferred height of the layout, the
preferredLayoutSize()
method sums up the preferred height of all the components intarget
along with thevgap
and top and bottom insets. For the preferred width,preferredLayoutSize()
returns the preferred width of the widest component. - public Dimension minimumLayoutSize (Container target)
- To get the minimum height of the layout, the
minimumLayoutSize()
method sums up the minimum height of all the components intarget
along with thevgap
and top and bottom insets. For the minimum width,minimumLayoutSize()
returns the minimum width of the widest component. - public void layoutContainer (Container target)
- The
layoutContainer()
method drawstarget
's components on the screen in one column. The width of each component is the width of the container. Each component's height is itspreferredSize()
height, if available.
- public String toString ()
- The
toString()
method ofVerticalBagLayout
returns a string with the current vertical gap setting. For example:
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
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
- public VariableGridLayout (int rows, int columns)
- This constructor creates a
VariableGridLayout
with the specified number ofrows
andcolumns
. You cannot specify zero for one dimension. If eitherrows
orcolumns
is zero, the constructor throws theNullPointerException
run-time exception. This constructor uses the default values for horizontal and vertical gaps (zero pixels), which means that components in adjacent cells will touch each other. - public VariableGridLayout (int rows, int columns, int hgap, int vgap)
- This version of the constructor is called by the previous one. It creates a
VariableGridLayout
with the specified number ofrows
andcolumns
, a horizontal gap ofhgap
, and a vertical gap ofvgap
. The gaps specify in pixels the space between adjacent components in the horizontal and vertical directions. It is possible to have negative gaps if you want components to overlap. You cannot specify zero for the number of rows or columns. If eitherrows
orcolumns
is zero, the constructor throws the run-time exceptionNullPointerException
.
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.
- public void setRowFraction (int rowNumber, double fraction)
- This method sets the percentage of space available for row
rowNumber
tofraction
. - public void setColFraction (int colNumber, double fraction)
- This method sets the percentage of space available for column
colNumber
tofraction
. - public double getRowFraction (int rowNumber)
- This method returns the current fractional setting for row
rowNumber
. - public double getColFraction (int colNumber)
- This method returns the current fractional setting for column
colNumber
.
The only method from GridLayout
that is overridden is the layoutContainer()
method.
- public void layoutContainer (Container target)
- The
layoutContainer()
method drawstarget
's components on the screen in a series of rows and columns. The size of each component within aVariableGridLayout
is determined by theRowFraction
andColFraction
settings for its row and column.
- public String toString ()
- The
toString()
method ofVariableGridLayout
returns a string with the current horizontal and vertical gap settings, the number of rows and columns, and the row and column fractional amounts. For example, the string produced by Figure 7.19 would be:
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
Constants
Since OrientableFlowLayout
subclasses FlowLayout
, the FlowLayout
constants of LEFT
, RIGHT
, and CENTER
are still available.
- public static final int HORIZONTAL
- The
HORIZONTAL
constant tells the layout manager to arrange components from left to right, like theFlowLayout
manager. - public static final int VERTICAL
- The
VERTICAL
constant tells the layout manager to arrange components from top to bottom. - public static final int TOP
- The
TOP
constant tells the layout manager to align the first component at the top of the screen (top justification). - public static final int BOTTOM
- The
BOTTOM
constant tells the layout manager to align the first component at the bottom of the screen (bottom justification).
- public OrientableFlowLayout ()
- This constructor creates a
OrientableFlowLayout
that acts like the defaultFlowLayout
. The objects flow from left to right and have anhgap
andvgap
of 5. - public OrientableFlowLayout (int direction)
- This constructor creates a
OrientableFlowLayout
in the givendirection
. Valid values areOrientableFlowLayout.HORIZONTAL
orOrientableFlowLayout.VERTICAL
. - public OrientableFlowLayout (int direction, int horizAlignment, int vertAlignment)
- This constructor creates a
OrientableFlowLayout
in the given direction. Valid values areOrientableFlowLayout.HORIZONTAL
orOrientableFlowLayout.VERTICAL
.horizAlignment
provides the horizontal alignment setting.vertAlignment
provides a vertical alignment setting; it may beOrientableFlowLayout.TOP
,FlowLayout.CENTER
, orOrientableFlowLayout.BOTTOM
. Ifdirection
isHORIZONTAL
, the vertical alignment is ignored. Ifdirection
isVERTICAL
, the horizontal alignment is ignored. - public OrientableFlowLayout (int direction, int horizAlignment, int vertAlignment, int horizHgap, int horizVgap, int vertHgap, int vertVgap)
- The final constructor adds separate horizontal and vertical gaps to the settings of
OrientableFlowLayout
. ThehorizHgap
andhorizVgap
parameters are the gaps when horizontally aligned. ThevertHgap
andvertVgap
parameters are the gaps when vertically aligned.
- public Dimension preferredLayoutSize (Container target)
- The
preferredLayoutSize()
method ofOrientableFlowLayout
calculates the preferred dimensions for thetarget
container. TheOrientableFlowLayout
computes the preferred size by placing all the components in one row or column, depending upon the current orientation, and adding their individual preferred sizes along with gaps and insets. - public Dimension minimumLayoutSize (Container target)
- The
minimumLayoutSize()
method ofOrientableFlowLayout
calculates the minimum dimensions for the container by adding up the sizes of the components. TheOrientableFlowLayout
computes the minimum size by placing all the components in one row or column, depending upon the current orientation, and adding their individual minimum sizes along with gaps and insets. - public void layoutContainer (Container target)
- The
layoutContainer()
method drawstarget
'sComponent
s on the screen, starting with the first row or column of the display, and going from left to right across the screen, or from top to bottom, based on the current orientation. When it reaches the margin of the container, it skips to the next row or column and continues drawing additional components.
- public void orientHorizontally ()
- The
orientHorizontally()
method allows you to change the orientation of theLayoutManager
to horizontal. The container must be validated before you see the effect of the change. - public void orientVertically ()
- The
orientVertically()
method allows you to change the orientation of theLayoutManager
to vertical. The container must be validated before you see the effect of the change. - public String toString ()
- The
toString()
method ofOrientableFlowLayout
returns a string with the current orientation setting, along with the entireFlowLayout.toString()
results. For example:
sun.awt.OrientableFlowLayout[orientation=vertical, sun.awt.OrientableFlowLayout[hgap=5,vgap=5,align=center]]