A
GridLayout
is a layout manager that divides a container into enough equal sized
rectangles, usually determined by the numbers assigned to the
rows
and
columns
fields, so that each component listed in the container's
layout
array can be placed in one of the rectangles.
Components should be AWT or Swing components, however all layout managers
build a label appropriate for the container they are working on when they
find a
String
in the layout array where they expected a component.
Properties, like the font or foreground color, used by these short-hand
labels can not be directly specified, so they are inherited from the
container.
The fields in a
GridLayout
are:
| columns |
An
int
that together with
model,
orientation,
and
useall
controls the number of columns in the grid.
A value of
0
(or even a negative number) means any number of columns.
| | hgap |
A
double
that represents the horizontal spacing between components in units
of 72 dots per inch.
| | model |
An
int
that picks one of three low level GridLayout algorithms.
In model
0,
which is the default,
rows
and
columns
both matter, but the actual grid size depends on the
orientation
and the number of components that need to be placed in the grid.
Model
1
is a variation of model
0
that uses
rows
and
columns,
rather than the number of components, to pick the minimum grid size.
Setting the model to
-1
selects the algorithm used in previous releases,
which matches what Java's AWT implementation provides.
In this model the value assigned to
columns
is completely ignored when
rows
is positive number.
| | orientation |
An
int
that helps control grid dimensions and determines how components
are arranged in that grid when the
model
is set to
0
or
1.
A value of
HORIZONTAL,
which is the default, means
columns
is fixed, components are added by row, and new rows are added
to the grid when necessary.
A value of
VERTICAL
means
rows
is fixed, components are added by column, and new columns are added
to the grid when necessary.
| | rows |
An
int
that together with
model,
orientation,
and
useall
controls the number of rows in the grid.
A value of
0
(or even a negative number) means any number of rows.
| | type |
A read-only
int
that is set to
GRIDLAYOUT,
which is defined in
yoix.awt
and
yoix.swing.
| | useall |
An
int
that is
1
when all components, whether visible or not, are used in sizing and
layout calculations, and
0
when only visible components are used.
The default value is
1,
which duplicates Java's GridLayout behavior,
but a value of
0
matches other layout managers and is required when your application
needs a GridLayout that adjusts when components are made visible or
invisible.
| | vgap |
A
double
that represents the vertical spacing between components in units
of 72 dots per inch.
|
No components use
GridLayout
as their default layout manager.
| |
| Example: |
The program,
import yoix.*.*;
JFrame f = {
BorderLayout layoutmanager = {
int vgap = 9;
};
Array layout = {
new JLabel {
String text = "GridLayout Test";
String font = "Helvetica-plain-14";
int alignment = CENTER;
},
NORTH,
new JTextArea {
String tag = "textarea";
Color background = Color.white;
},
CENTER,
new JPanel { // XXX
Array layout = { // XXX
new JPanel {
GridLayout layoutmanager;
Array layout = {
new JButton {
String text = "Update";
},
new JButton {
String text = "Clear";
actionPerformed(e) {
root.components.textarea.text = "";
}
},
new JButton {
String text = "Quit";
actionPerformed(e) {
exit(0);
}
},
};
},
}; // XXX
}, // XXX
SOUTH,
};
};
f.visible = TRUE;
uses a
GridLayout
to make three buttons that are the same size,
and two nested panels so the buttons are not
bigger than they really need to be.
Remove the four lines marked by
XXX,
and the buttons will be the same size, because they are still in a
GridLayout,
but they are much bigger than they need to be.
Do you understand why?
| | |
| See Also: |
BorderLayout,
BoxLayout,
CardLayout,
CustomLayout,
FlowLayout,
GridBagLayout,
LayoutManager
|
|