Java Reference
In-Depth Information
Tip Using the BEFORE and AFTER constants, as opposed to the NORTH , SOUTH , EAST , and WEST
constants, is recommended, though all are supported.
You do not need specify all five areas of the container. The component in the north region
takes up the entire width of the container along its top. South does the same along the bottom.
The heights of north and south will be the preferred heights of the component added. The east
and west areas are given the widths of the component each contains, where the height is whatever
is left in the container after satisfying north's and south's height requirements. Any remaining
space is given to the component in the center region.
The way to place multiple components into one of the regions of a BorderLayout -managed
container is to add them to a different container first, and then add them to the BorderLayout -
managed container. For instance, if you want a label and text field in the north area of
a BorderLayout -managed container, place them in the west and center areas of another
BorderLayout -managed container first, as shown here:
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Name:");
JTextField text = new JTextField();
topPanel.add(label, BorderLayout.BEFORE_LINE_BEGINS);
topPanel.add(text, BorderLayout.CENTER);
outerPanel.add(topPanel, BorderLayout.BEFORE_FIRST_LINE);
GridLayout Class
The GridLayout manager is ideal for laying out objects in rows and columns, where each cell in
the layout has the same size. Components are added to the layout from left to right, top to
bottom. A call to setLayout(new GridLayout(3, 4)) changes the layout manager of the current
container to a GridLayout with three rows and four columns, as shown in Figure 10-4.
Figure 10-4. Sample GridLayout
 
Search WWH ::




Custom Search