Docsity

Understanding GUI Layout Managers in Java: Flow, Grid, Border, and More, Slides of Java Programming

Partial preview of the text

Download Understanding GUI Layout Managers in Java: Flow, Grid, Border, and More and more Slides Java Programming in PDF only on Docsity!

GUI Layout Managers

Outline

• Components and Containers

• Layout Managers

• Flow Layout

• Grid Layout

• Border Layout

• Nested Containers with Layouts

• Overview of Advanced Layout Managers

GUI component classes in AWT

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.

Flow Layout

  • The Flow Layout manager arranges the components left-to-

right, top-to-bottom in the order they were inserted into the container.

  • 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.

Flow Layout Examples

Flow Layout Alignment

• The line alignment can be:

  • FlowLayout.LEFT
  • FlowLayout.CENTER
  • FlowLayout.RIGHT
  • FlowLayout.LEADING
  • FlowLayout.TRAILING

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.

Grid Layout Constructors

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. GridLayout()

A single row and no vertical or horizontal gaps.

Grid Layout - Notes

• Important constructor notes:

  • Parameter r (number of rows) or c (number of

columns) can be equal to 0, then the grid can have

any number of rows, or columns, respectively,

depending on the number of components in the

container.

  • Both r and c cannot be made 0 at the same time.

Border Layout Arrangement

  • If one or more of the components, except the Center

component, are missing then the rest of the existing components are stretched to fill the remaining space in the container.

Border Layout Constructors

BorderLayout(hgap, vgap)

hgap – horizontal gaps between components

vgap – vertical gaps between components

BorderLayout()

No vertical or horizontal gaps.

Nested Containers with Layouts

  • Each of the nested containers can have the

same type or totally different type of a layout

manager.

  • Outer Layout: Border Layout
  • Inner Layouts:

Border Layout

Flow Layout

Overview of Advanced Layout

Managers

  • Box Layout Manager
    • The Swing packages include a general purpose

layout manager named Box Layout manager. Box

Layout can either stack its components on top of

each other or place them in a row.

  • One big difference between Box Layout and many

earlier layout managers is that Box Layout

respects each component's maximum size and X/Y

alignment.