Layout managers
They are used in GUI components in a container for presentation purposes and also basic layout
capabilities. This functionality enables the programmer to concentrate on the basic look-and -feel and lets
the layout managers process most of the layout details. For example, when placing a button location in a
window using hard coded pixel measurements, the user interface might look fine on one system but be
unusable on another. Java layout managers provide a level of abstraction that automatically maps your
user interface on all window systems.
In the preceding program, you did not specify where to place the OK button in the frame, but Java knows
where to place it, because the layout manager works behind the scenes to place components in the correct
locations. A layout manager is created using a layout manager class.
Layout managers are set in containers using the setLayout (aLayoutManager) method. For example, you
can use the following statements to create an instance of XLayout and set it in a container:
LayoutManager layoutManager = new XLayout();
[Link](layoutManager);
There are three basic layout managers which are:
FlowLayout
GridLayout
BorderLayout
Absolute Positioning
This provides the greatest level of control over a GUI appearance. By setting a containers layout to null,
you can specify the absolute position of each GUI component with respect to the upper-left corner of the
container. When using absolute positioning also must specify each GUI components size. Programming a
GUI with absolute positioning can be tedious, unless you have an integrated development environment
(IDE) that can generate the code for you.
FlowLayout
FlowLayout is the simplest layout manager. The components are arranged in the container from left to
right in the order in which they were added. When one row is filled, a new row is started. You can specify
the way the components are aligned by using one of three constants: [Link],
[Link], or [Link]. You can also specify the gap between components in
pixels. It is also possible to specify the order of the components by using the container method add, which
takes a component and an integer index position as arguments.
GridLayout
The GridLayout manager arranges components in a grid (matrix) formation. The components are placed
in the grid from left to right, starting with the first row, then the second, and so on, in the order in which
they are added.
You can specify the number of rows and columns in the grid. The basic rules are as follows:
The number of rows or the number of columns can be zero, but not for both. If one is zero and the
other is nonzero, the nonzero dimension is fixed, while the zero dimension is determined
dynamically by the layout manager
If both the number of rows and the number of columns are nonzero, the number of rows is the
dominating parameter; that is, the number of rows is fixed, and the layout manager dynamically
calculates the number of columns
BorderLayout
The BorderLayout manager divides a container into five areas: East, South, West, North, and Center.
Components are added to a BorderLayout by using add(Component, index), where index is a constant
[Link], [Link],[Link], [Link], or
[Link]. The components are laid out according to their preferred sizes and their
placement in the container.
BoxLayout