Java Reference
In-Depth Information
set the size and position of each one, using setBounds() . layoutContainer() can
call preferredSize() on each component to determine the size it would like to
be.
The other important method is preferredLayoutSize() . This method should
return the preferred size of the container. Typically this is the size required to
arrange all the components at their preferred sizes. The minimumLayoutSize()
method is similar, in that it should return the minimum allowable size for the con-
tainer. Finally, if your layout manager is interested in constraints specified when
the add() method is called to add a component to a container, it can define the
addLayoutComponent() method.
Example 10-9 shows a listing of ColumnLayout.java , an implementation of the
LayoutManager2 interface that arranges components in a column. ColumnLayout
differs from BoxLayout in that it allows a horizontal alignment to be specified for
the components in the column. Example 10-10 is a simple program that uses
ColumnLayout to produce the output shown in Figure 10-9.
Figure 10•9. Component laid out with a custom layout manager
Example 10•9: ColumnLayout.java
package com.davidflanagan.examples.gui;
import java.awt.*;
/**
* This LayoutManager arranges the components into a column.
* Components are always given their preferred size.
*
* When you create a ColumnLayout, you may specify four values:
*
margin_height -- how much space to leave on top and bottom
*
margin_width -- how much space to leave on left and right
*
spacing -- how much vertical space to leave between items
*
alignment -- the horizontal position of the components:
Search WWH ::




Custom Search