0% found this document useful (0 votes)
55 views9 pages

Layouts

Layout managers control the positioning of components within containers in Java. The four basic layout managers are FlowLayout, GridLayout, BorderLayout, and CardLayout. FlowLayout positions components left-to-right, top-to-bottom. GridLayout positions components in a grid with equal sizes. BorderLayout positions components in north, south, east, west, and center areas. CardLayout positions components in layers like cards that can be flipped between.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views9 pages

Layouts

Layout managers control the positioning of components within containers in Java. The four basic layout managers are FlowLayout, GridLayout, BorderLayout, and CardLayout. FlowLayout positions components left-to-right, top-to-bottom. GridLayout positions components in a grid with equal sizes. BorderLayout positions components in north, south, east, west, and center areas. CardLayout positions components in layers like cards that can be flipped between.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Layout Managers

Layout Managers
• Each container has a layout manager, which controls the
way the components are positioned in the container.
• One of the advantages of using layout managers is that
there is no need for absolute coordinates where each
component is to be placed or the dimensions of the
component.
• The layout manager automatically handles the calculation
of these.
• Programmer only specifies relative positions of the
components within the container.
• Whenever the dimensions of the container change (e.g.
user resizes the window), layout manager recalculates the
absolute coordinates of components for them to fit the
new container size.
• There are four basic layout managers are
available in java.awt.package:
• FlowLayout
• BorderLayout
• GridLayout
• CardLayout
• GridbagLayout
Flow Layout
• The Flow Layout manager arranges the components
left-to-right, top-to-bottom in the order they were
inserted into the container.
• Components are added to the container first come first
basis.
• When the container is not wide enough to display all
the components, the remaining components are
placed in the next row, etc.
• Each row is centered. And all components are sized to
preferred size and maintains a gap of 5 pixels.
• This layout is default layout for Panel, Applet.
Grid Layout
• The Grid Layout manager lays out all the components in a
rectangular grid.
• All the components have identical sizes, since the manager
automatically stretches or compresses the components as to
fill the entire space of the container.
• Constructors are:
• GridLayout(r, c, hgap, vgap)
– r – number of rows in the layout
– c – number of columns in the layout
– hgap – horizontal gaps between components
– vgap – vertical gaps between components
• GridLayout(r, c)
– r – number of rows in the layout
– c – number of columns in the layout
– No vertical or horizontal gaps.
Border Layout
• The Border Layout manager arranges components into five
regions: (4 borders + center)
– Top border – BorderLayout.NORTH
– Left border – BorderLayout.WEST
– Right border – BorderLayout.EAST
– Bottom border – BorderLayout.SOUTH
• This layout is so useful to arrange menubar at top, vertical
scrollbar at right border, and horizontal scrollbar at bottom
border.
• Components in the North and South are set to their natural
heights and horizontally stretched to fill the entire width of the
container.
• Components in the East and West are set to their natural widths
and stretched vertically to fill the entire width of the container.
• The Center component fills the space left in the center of the
container.
• This layout is default layout for Frame and dialog.
Card Layout
• In card layout, components are added in layers.(one
component is on the top of another components, like pack of
cards)
• Using this layout, we can hide dome components which are
necessary to display.
• Cardlayout has different methods to switch from one card to
another card.
– public void next(Container parent): is used to flip to the next card
of the given container.
– public void previous(Container parent): is used to flip to the
previous card of the given container.
– public void first(Container parent): is used to flip to the first card of
the given container.
– public void last(Container parent): is used to flip to the last card of
the given container.
– public void show(Container parent, String name): is used to flip to
the specified card with the given name.
Gridbag Layout
• We can arrange components in horizontal as well in vertical
direction or by positioning them within a cell of a grid
• Components need not be of same size in a row.
• Each row can contain dissimilar number of columns.
• GridBagConstraint contains the constraint which includes
the
– height, width of a cell, placement and alignment of components.
• GridBagLayout object maintains a rectangular grid of cell.
• Component can occupy one or more cells, a.k.a its display
area.
• ComponentOrientation class controls the orientation of the
grid.
• We need to customize GridBagConstraints objects to use
GridBagLayout effectively associated with its components.

You might also like