Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Class Declaration
3.
Field
4.
Example
4.1.
Output
5.
Constructors
6.
Methods of BorderLayout in Java
7.
Example 
7.1.
Output
8.
Frequently Asked Questions
8.1.
What is the border layout manager in Java?
8.2.
What is the default layout in Java?
8.3.
What is the grid layout in Java?
8.4.
What are layouts in Java?
8.5.
What is the purpose of setLayout in Java?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

BorderLayout In Java

Author Kanak Rana
0 upvote

Introduction

BorderLayout in Java is also known as Layout Manager. Its work is to control the layout, i.e., the size and position of the components. Also, the default BorderLayout in Java has been used to layout a container (north, east, south, etc.), i.e., arrange and resize its components.

BorderLayout class is used by window objects such as JFrame, JWindow, and JDialog to display GUI(Graphic User Interface) components. BorderLayout is used to arrange and resize the components of a container. 

border layout in java

The border layout divides these elements into five distinct regions. Four of the five regions or areas are referred to as north, south, east, and west, with the middle region referred to as the center. Each region can only have one component. The border layout includes a set of constants for positioning components.

border layout in java

In addition to the constants(NORTH, SOUTH, EAST, WEST) listed above(in the figure), BorderLayout provides additional positioning constants such as PAGE_START, PAGE_END, and so on.

Class Declaration

Declaration for java.awt.BorderLayout class −

public class BorderLayout
extends Object
implements LayoutManager2, Serializable

Field

Following are the fields for java.awt.BorderLayout class:

Field of BorderLayout Class

Example

By using a constructor name BorderLayout()

// BorderLayout In Java

import java.awt.*;  
import javax.swing.*;    
   
public class Border_lines
{    
	JFrame j;    
	Border()  
	{
		j = new JFrame();
		
		// Creating buttons
		JButton a1 = new JButton("NORTH");
		JButton a2 = new JButton("SOUTH");
		JButton a3 = new JButton("EAST");
		JButton a4 = new JButton("WEST");
		JButton a5 = new JButton("CENTER(Welcome to Coding Ninjas)");
		
		j.add(a1, BorderLayout.NORTH);
		j.add(a2, BorderLayout.SOUTH);
		j.add(a3, BorderLayout.EAST);
		j.add(a4, BorderLayout.WEST);
		j.add(a5, BorderLayout.CENTER);
		
		j.setSize(350, 350);
		j.setVisible(true);	
	}
	public static void main(String[] args)
	{
		new Border();
	}    
}
You can also try this code with Online Java Compiler
Run Code

Output

Output


Following classes that represent the layout managers:

  • java.awt.BorderLayout
  • java.awt.FlowLayout
  • java.awt.GridLayout
  • java.awt.CardLayout
  • java.awt.GridBagLayout
  • javax.swing.BoxLayout etc.

Constructors

  • BorderLayout() creates a new border layout with no gaps between the components.
     
  • BorderLayout (int hgap, int vgap) creates a border layout with the specified spacing between components.
    Where, hgap is the horizontal gap and vgap is the vertical gap.

Methods of BorderLayout in Java

Methods

Description

int getVgap() Returns the distance between the components' vertical axes.
void layout container(Container target) This border layout is used to lay out the container argument.
void addLayoutComponent (Component comp, Object constraints) Uses the specified constraint object to add the specified component to the layout.
void addLayoutComponent (String name, Component comp) If the layout manager uses a per-component string, it adds the component comp to the layout. It associates it with the string specified by name.
Dimension maximum layout size(Container target) Give the components in the specified target container, this layout's maximum dimensions are returned.
Dimension minimum layout size(Container target) This layout manager determines the minimum size of the target container.
String toString() This method returns a string representation of the current state of this border layout.
void setHgap(int hgap) Determines the horizontal distance between the components.

Example 

// BorderLayout In Java

import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;

public class border extends Frame
{
   private static final long serialVersionUID = 2L;
   public border(String title)
   {
    	super(title);
    	setLayout(new BorderLayout(50,60));
 
    	JButton a1=new JButton("North");
    	add(a1,BorderLayout.NORTH);
 
    	JButton a2=new JButton("EAST");
    	add(a2,BorderLayout.EAST);
 
    	JButton a3=new JButton("CENTER(Welcome to Coding Ninjas)");
    	add(a3,BorderLayout.CENTER);
 
    	JButton a4=new JButton("WEST");
    	add(a4,BorderLayout.WEST);
 
    	JButton a5=new JButton("SOUTH");
    	add(a5,BorderLayout.SOUTH);
    }
    
    // Driver Code
    public static void main(String[] args)
    {
    	// Call the constructor
    	border screen = new border("BorderLayout (Code studio)");
    
    	//Function for size of the Frame
    	screen.setSize(360,380); 
    
    	//Function to  visible status of the frame
    	screen.setVisible(true);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Output

Frequently Asked Questions

What is the border layout manager in Java?

The BorderLayout is used to organize the components into five distinct regions: north, south, east, west, and center. Each region (area) may only have one component. It is the standard frame or window layout.

What is the default layout in Java?

The FlowLayout is the default layout. It layout the components in a directional flow.

What is the grid layout in Java?

The GridLayout class is a layout manager that arranges the components of a container in a rectangular grid. The container is divided into equal-sized rectangles, with one component in each.

What are layouts in Java?

The arrangement of components within the container is referred to as layout. In other words, the components are placed in a specific location within the container. The Layout Manager handles the task of layout and the controls automatically.

What is the purpose of setLayout in Java?

The setLayout(...) method lets you specify the layout of the container, which is usually a JPanel, such as FlowLayout, BorderLayout, GridLayout, null layout, or whichever layout you want.

Conclusion

This blog covered BorderLayout in Java, where we also covered the constructors and methods of the layout manager. If you would like to learn more, check out these articles on the following to have a better understanding of java:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Thank you

Happy Learning Ninja! 

Live masterclass