Updated July 4, 2023
Introduction to BorderLayout in Java
BorderLayout in Java or the Layout manager is an object that every container object has, which controls its layout i.e. size and position of the components. In layman language, these Layout Managers are used to arrange the components in a particular or specific manner. ‘LayoutManager’ can be said as an interface that is implemented by all of its classes. There are some AWT and SWING classes that have been provided as layout managers for general use. Some of them are listed below:
- BorderLayout
- BoxLayout
- SpringLayout
- FlowLayout
- GroupLayout
- GridLayout, etc
Before moving to BorderLayout lets suppose to discuss some points.
Almost everything in the computer world is hierarchically organized either for better storage or for creating an easy retrieval system. Layout Managers used for creating GUIs using available AWT classes are a part of one such hierarchical system. It goes by the name Containment Hierarchy. Though this article’s focus will be on one of the available LayoutManager known as BorderLayout, before that we should know where BorderLayout stands in this Hierarchy. Our tree structure that runs through frames, GUI components, containers, etc. Starts with:
- First, a top-level container or top-level container class such as JFrame, JApplet or JDialog, which acts as the ‘ROOT’ of this containment hierarchy.
- Second, for a component to appear as a GUI component on the screen, it must be included in the containment hierarchy.
- Third, one component needs to be or will be contained in one container only.
- Fourth, each top-level container will have a content pane, loosely speaking, every GUI visible (directly or indirectly) are contained in this content pane.
What is BorderLayout?
A BorderLayout class is one of the layout manager classes which is considered to be the default layout for window objects. The window objects such as JFrame, JWindow, JDialog use this class to be displayed as GUI components. BorderLayout is used to layout a container i.e. arrange and resize its components. The border layout arranges these components into five different regions. Out of five, four regions or areas are referred to as north, south, east and west and the middle region is referred to as the center. Each region may contain only one component. The border layout provides a collection of constants used for positioning components. The regions discussed here identified by a corresponding constant named NORTH, SOUTH, EAST, WEST, and the constant for the middle region as CENTER. We use these constants while adding a component using a border layout. Constants for each region used in our code are listed below:
- public static final int NORTH: The North layout constant at top of the container.
- public static final int SOUTH: The South layout constant at bottom of the container.
- public static final int CENTER: The Center layout constant in the middle of the container.
- public static final int WEST: The West layout constant at the right of the container.
- public static final int EAST: The East layout constant at the left of the container.
In addition to the above-listed constants, BorderLayout provides other positioning constants too such as PAGE_START, PAGE_END, etc.
BorderLayout Constructors in Java
BorderLayout Constructors are used to creating new border layouts with gaps or with no gaps between the components. The border layout constructor use gap and gap parameters to specify horizontal gaps and vertical gaps between the components. The border layout constructors are discussed below:
We will discuss them one by one along with their examples.
1. BorderLayout ( )
BorderLayout Class is used to create a border layout but without gaps between the components. Following is an example code using the BorderLayout() constructor. The following program creates components in a JFrame whose instance class is ‘border’. You can see in the screenshot below (after the code), there are five regions.
Our code has created five regions using five JButton and then these are added to the layout or JFrame using an add() method. The size and visibility are determined separately for the JFrame. setSize() method is used to set the size for the frame and the set visibility() method is being used to set the visibility of the frame.
Code
//Java Program illustrating the BorderLayout
import java.awt.*;
import javax.swing.*;
public class border
{
JFrame JF;
border()
{
JF=new JFrame(); //JFrame object
//Lying at top, will be the button named 'North'
JButton b1=new JButton("NORTH");
//Lying at bottom, will be the button named 'South'
JButton b2=new JButton("SOUTH");
//Lying at left, will be the button named 'East'
JButton b3=new JButton("EAST");
//Lying at right, will be the button named 'West'
JButton b4=new JButton("WEST");
//In the center, will be the button named 'Center'
JButton b5=new JButton("CENTER");
//Adding our buttons
JF.add(b1,BorderLayout.NORTH);
JF.add(b2,BorderLayout.SOUTH);
JF.add(b3,BorderLayout.EAST);
JF.add(b4,BorderLayout.WEST);
JF.add(b5,BorderLayout.CENTER);
//Function to set size of the Frame
JF.setSize(300, 300);
//Function to set visible status of the frame
JF.setVisible(true);
}
//Driver Code
public static void main(String[] args)
{
//Calling the constructor
new border();
}
}
In the below screenshot, you can see how the code looks like in the text editor. I have used Eclipse as my text editor.
When the code runs, the following output is displayed.
Output:
2. BorderLayout (int hgap, int vgap)
BorderLayout (int hgap, int vgap) is also written as BorderLayout (int, int) is used to create a border layout with the given horizontal (hgap) and vertical (vgap) gaps or spaces between the following is an example code using BorderLayout (int, int) constructor. The following program creates components in a JFrame whose instance class is ‘border’ in a similar way as above but with vertical and horizontal gaps. You can see in the screenshot below (after the code).
Our code has created five regions with gaps between them using vgap and hgap parameters in BorderLayout( ). The size and visibility are again determined separately for the JFrame. setSize() method is used to set the size for the frame and the set visibility() method is being used to set the visibility of the frame.
You can see the code for generating a frame using BorderLayout with vgap and hgap parameters, creating gaps or spaces between the regions is shown below:
Code
//Java Program illustrating the BorderLayout with SPACES between COMPONENTS
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
public class borderlayout_spaces extends Frame
{
public borderlayout_spaces(String title)
{
super(title);
setLayout(new BorderLayout(20,30));
//setBackground(Color.RED);
//Lying at top, will be the button named 'North'
JButton b1=new JButton("North");
add(b1,BorderLayout.NORTH);
//Lying at left, will be the button named 'East'
JButton b2=new JButton("EAST");
add(b2,BorderLayout.EAST);
//In the center, will be the button named 'Center'
JButton b3=new JButton("CENTER");
add(b3,BorderLayout.CENTER);
//Lying at right, will be the button named 'West'
JButton b4=new JButton("WEST");
add(b4,BorderLayout.WEST);
//Lying at bottom, will be the button named 'South'
JButton b5=new JButton("SOUTH");
add(b5,BorderLayout.SOUTH);
}
//Driver Code
public static void main(String[] args)
{
//Calling the constructor
borderlayout_spaces screen =
new borderlayout_spaces("Border Layout");
screen.setSize(300,300); //Function to set size of the Frame
screen.setVisible(true); //Function to set visible status of
the frame
}
}
In the below screenshot, you can see how the code looks like in the text editor.
The code line, “setLayout(new BorderLayout(20,30));” sets out the output as the image shown below. Here 20 is the horizontal space and 30 represents the vertical space.
When the code runs, the following output is displayed.
Output:
Also, you can try other properties, for example, set background color. In the above code, if you see a commented code line as “setBackground(Color.RED);” if you try this code with this line uncommented, your frame will appear as below:
There are other commonly used methods alongside border layout, such as:
- toString(): Returns a string which is a representation of the state of border layout.
- getVgap(): Return the vertical gap between the component.
- getHgap(): Return the horizontal gap between the component.
- setVgap(int vgap): This is used to SET the vertical gap between the component.
- setHgap(int hgap): This is used to SET the horizontal gap between the components, etc.
This article discussed some of the powerful LayoutManagers that are used to lay out your windows frames and GUIs. The article also covered writing code layouts using two different constructors known as java.awt.BorderLayout class and java.awt.BorderLayout(int, int) class by hand, which could have been challenging. But you should try and give it go. If not, you can try using java.awt.GroupLayout layout manager. You can use a building tool too along with your layout manager, for example, NetBeans IDE, which is a great tool to create your GUI.
Recommended Articles
This is a guide to BorderLayout in Java. Here we discuss an introduction to BorderLayout in Java, BorderLayout Constructors with codes and outputs. You can also go through our other related articles to learn more-