Select Page

The BorderLayout Manager

by | May 27, 2023 | Layouts, Swing | 0 comments

The BorderLayout manager divides a container into five regions. The regions are north, east, south, west and center. The BorderLayout is a default layout for window objects suchs the JFrame, JWindow, JDialog, JInternalFrame etc.

Template of the BorderLayout Manager

BorderLayout

When you add a component to a container that is managed by a Border layout, the component has to be placed in one of the five regions of the layout. You can place only one component at each of the five regions.

Constructors of the BorderLayout

(1) BorderLayout(): This constructor constructs a border layout with no gaps between the components.

(2) BorderLayout(int horizontal gap, int vertical gap) : This constructs a border layout with the specified gaps between the components.

Commonly Used Methods

(1) getLayoutAlignmentX(Container parent) : Returns the layout alignment along the X axis

(2) getLayoutAlignmentY(Container parent) : returns the layout alignment along the Y axis

(3) removeLayoutComponent(Component comp) : removes a specified component from the boderlayout

(4) getHgap() : returns the horizontal gap between the components

(5) getVgap() : returns the vertical gap between the components

(6) setHgap(int hgap) : sets the horizontal gap between the components

(7) setVgap(int vgap) : sets the vertical gap between the components

Rules of the BorderLayout

(1) Each region can hold only one component

(2) When a component is added to a region, it is stretched to fill the entire region

Program to demonstrate how components are added to a Border layout

Output

BorderWindow1

When you don’t specify a region for a component

If you add a component to a Border layout container without specifying the region, the component is by default added to the center region. In other words, the statement

is equivalent to

If you add more components without specifying the region, the components are stacked on one another at the center region and the window will show only the latest component added.

Program to demonstrate that adding components without specifying region stacks them at the center region

Output

BorderWindowStackedAtCENTER

Rules of the BorderLayout Manager

  1. A component placed in either the north or south region can be resized horizontally to fill the entire region. The north and south regions are vertically aligned and they can be resized horizontally.
  2. A component placed in either the east or west regions can be resized vertically so that it fills up the entire region.
  3. A component placed in the center region may be resized both horizontally and vertically so it fills up the entire region.

Horizontal and Vertical gaps between components in a border layout

The regions in a BorderLayout object do not have any gaps between them. The horizontal gap can be set using the setHgap(int hgap) method. The vertical gap can be set using the setVgap(int vgap) method.

Alternatively, you can use an overloaded version of the BorderLayout constructor to specify horizontal and vertical gaps.

Example:

//border layout is created with 5 pixels horizontally and 10 pixels vertically

Alternative names to describe position of region in border layout

BorderLayout allows the use of relative positioning constants to define the position of components.

Note: Do not mix the absolute positioning constants with the relative positioning constants

Program to demonstrate the use of PAGE_START, PAGE_END, LINE_START, LINE_END

Output

BorderWindow2

Program to demonstrate the use of the BEFORE_FIRST_LINE, AFTER_LAST_LINE, BEFORE_LINE_BEGINS, AFTER_LINE_ENDS

Output

BorderWindow3