FlowLayout
FlowLayout
is the default LayoutManager
for a Panel
. A FlowLayout
adds components to the container in rows, working from left to right. When it can't fit any more components in a row, it starts a new row--not unlike a word processor with word wrap enabled. When the container gets resized, the components within it get repositioned based on the container's new size. If sufficient space is available, components within FlowLayout
containers are given their preferred size. If there is insufficient space, you do not see the components in their entirety.
FlowLayout Methods
ConstantsFlowLayout
defines three constants, all of which are used to specify alignment. The alignment tells FlowLayout
where to start positioning the components on each row. Each component is still added from left to right, no matter what the alignment setting is.
- public final static int LEFT
LEFT
is the constant for left alignment.- public final static int CENTER
CENTER
is the constant for center alignment and is the default.- public final static int RIGHT
RIGHT
is the constant for right alignment.
- public FlowLayout ()
- This constructor creates a
FlowLayout
using default settings: center alignment with a horizontal and vertical gap of five pixels. The gap is the space between the different components in the different directions. By default, there will be five pixels between components. The constructor is usually called within a call tosetLayout()
:setLayout (new FlowLayout())
. Figure 7.1 shows how the defaultFlowLayout
behaves with different screen sizes. As the screen C shows, if the screen is too small, the components will not be shrunk so that they can fit better.
Figure 7.1: FlowLayout with six buttons and three different screen sizes
- public FlowLayout (int alignment)
- This version of the constructor creates a
FlowLayout
using the specifiedalignment
and a horizontal and vertical gap of five pixels. Valid alignments are theFlowLayout
constants, although there is no verification. Figure 7.2 shows the effect of different alignments:FlowLayout.LEFT
(screen A),FlowLayout.CENTER
(B), andFlowLayout.RIGHT
(C).
Figure 7.2: FlowLayout with three different alignments
- public FlowLayout (int alignment, int hgap, int vgap)
- The final version of the constructor is called by the other two. It requires you to explicitly specify the alignment, horizontal gap (
hgap
), and vertical gap (vgap
). This creates aFlowLayout
with an alignment ofalignment
, horizontal gap ofhgap
, and vertical gap ofvgap
. The units for gaps are pixels. It is possible to have negative gaps if you want components to be placed on top of one another. Figure 7.3 shows the effect of changing the gap sizes.
Figure 7.3: FlowLayout with hgap of 0 and vgap of 20
Informational methods
- public int getAlignment ()
- The
getAlignment()
method retrieves the current alignment of theFlowLayout
. The return value should equal one of the class constantsLEFT
,CENTER
, orRIGHT
. - public void setAlignment (int alignment)
- The
setAlignment()
method changes theFlowLayout
alignment toalignment
. The alignment value should equal one of the class constantsLEFT
,CENTER
, orRIGHT
, but this method does not check. After changing the alignment, you mustvalidate()
theContainer
. - public int getHgap ()
- The
getHgap()
method retrieves the current horizontal gap setting. - public void setHgap (int hgap)
- The
setHgap()
method changes the current horizontal gap setting tohgap
. After changing the gaps, you mustvalidate()
theContainer
. - public int getVgap ()
- The
getVgap()
method retrieves the current vertical gap setting. - public void setVgap (int hgap)
- The
setVgap()
method changes the current vertical gap setting tovgap
. After changing the gaps, you mustvalidate()
theContainer
.
- public void addLayoutComponent (String name, Component component)
- The
addLayoutComponent()
method ofFlowLayout
does nothing. - public void removeLayoutComponent (Component component)
- The
removeLayoutComponent()
method ofFlowLayout
does nothing. - public Dimension preferredLayoutSize (Container target)
- The
preferredLayoutSize()
method ofFlowLayout
calculates the preferred dimensions for thetarget
container. TheFlowLayout
computes the preferred size by placing all the components in one row and adding their individual preferred sizes along with gaps and insets. - public Dimension minimumLayoutSize (Container target)
- The
minimumLayoutSize()
method ofFlowLayout
calculates the minimum dimensions for the container by adding up the sizes of the components. TheFlowLayout
computes the minimum size by placing all the components in one row and adding their individual minimum sizes along with gaps and insets. - public void layoutContainer (Container target)
- The
layoutContainer()
method drawstarget
's components on the screen, starting with the first row of the display, going left to right across the screen, based on the current alignment setting. When it reaches the right margin of the container, it skips down to the next row, and continues drawing additional components.
- public String toString ()
- The
toString()
method ofFlowLayout
returns the current horizontal and vertical gap settings along with the alignment (left, center, right). For aFlowLayout
that uses all the defaults,toString()
produces:
java.awt.FlowLayout[hgap=5,vgap=5,align=center]