Skip to main content

java.awt.BorderLayout in java

Java's AWT (Abstract Window Toolkit) provides several layout managers to help developers create graphical user interfaces. One of the most commonly used layout managers is BorderLayout. This layout manager is used to divide the container into five regions: North, South, East, West, and Center. Each region can hold one component at a time.

BorderLayout is a powerful layout manager that can be used to create a wide range of GUI designs. In this article, we will explore the various methods and features of the BorderLayout class, and provide a code example that demonstrates its capabilities.

public class java.awt.BorderLayout
implements java.awt.LayoutManager2,
	int hgap;
	int vgap;
	java.awt.Component north;
	java.awt.Component west;
	java.awt.Component east;
	java.awt.Component south;
	java.awt.Component center;
	java.awt.Component firstLine;
	java.awt.Component lastLine;
	java.awt.Component firstItem;
	java.awt.Component lastItem;
	public static final java.lang.String NORTH;
	public static final java.lang.String SOUTH;
	public static final java.lang.String EAST;
	public static final java.lang.String WEST;
	public static final java.lang.String CENTER;
	public static final java.lang.String BEFORE_FIRST_LINE;
	public static final java.lang.String AFTER_LAST_LINE;
	public static final java.lang.String BEFORE_LINE_BEGINS;
	public static final java.lang.String AFTER_LINE_ENDS;
	public static final java.lang.String PAGE_START;
	public static final java.lang.String PAGE_END;
	public static final java.lang.String LINE_START;
	public static final java.lang.String LINE_END;
	public java.awt.BorderLayout();
	public java.awt.BorderLayout(int, int);
	public int getHgap();
	public void setHgap(int);
	public int getVgap();
	public void setVgap(int);
	public void addLayoutComponent(java.awt.Component, java.lang.Object);
	public void addLayoutComponent(java.lang.String, java.awt.Component);
	public void removeLayoutComponent(java.awt.Component);
	public java.awt.Component getLayoutComponent(java.lang.Object);
	public java.awt.Component getLayoutComponent(java.awt.Container, java.lang.Object);
	public java.lang.Object getConstraints(java.awt.Component);
	public java.awt.Dimension minimumLayoutSize(java.awt.Container);
	public java.awt.Dimension preferredLayoutSize(java.awt.Container);
	public java.awt.Dimension maximumLayoutSize(java.awt.Container);
	public float getLayoutAlignmentX(java.awt.Container);
	public float getLayoutAlignmentY(java.awt.Container);
	public void invalidateLayout(java.awt.Container);
	public void layoutContainer(java.awt.Container);
	public java.lang.String toString();

Overview of BorderLayout Methods

The BorderLayout class provides several methods that can be used to manipulate the layout manager's behavior. Here is an overview of the most commonly used methods:

  1. addLayoutComponent(Component comp, Object constraints) - This method adds the specified component to the layout, using the specified constraints object.

  2. getLayoutAlignmentX(Container target) - This method returns the alignment along the x axis for the layout.

  3. getLayoutAlignmentY(Container target) - This method returns the alignment along the y axis for the layout.

  4. getLayoutComponent(Container target, Object constraints) - This method returns the component that is associated with the specified constraints object.

  5. getLayoutComponents(Container target) - This method returns an array of the components that are associated with this layout.

  6. layoutContainer(Container parent) - This method lays out the container according to the rules of the BorderLayout layout manager.

  7. maximumLayoutSize(Container target) - This method returns the maximum size of the container, given the components that are associated with this layout.

  8. minimumLayoutSize(Container target) - This method returns the minimum size of the container, given the components that are associated with this layout.

  9. preferredLayoutSize(Container target) - This method returns the preferred size of the container, given the components that are associated with this layout.

  10. removeLayoutComponent(Component comp) - This method removes the specified component from the layout.

Code Example

Let's take a look at a code example that demonstrates the capabilities of the BorderLayout class. In this example, we will create a simple GUI that contains five buttons: one in each of the North, South, East, West, and Center regions. We will use the methods of the BorderLayout class to configure the layout.

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.*;
public class Main {

    public static void main(String[] args) {
        // Create the JFrame
        JFrame frame = new JFrame("BorderLayout Example");
        frame.setSize(400, 400);

        // Create the JPanel
        JPanel panel = new JPanel(new BorderLayout());

        // Create the buttons
        JButton northButton = new JButton("North");
        JButton southButton = new JButton("South");
        JButton eastButton = new JButton("East");
        JButton westButton = new JButton("West");
        JButton centerButton = new JButton("Center");

        // Add the buttons to the panel
        panel.add(northButton, BorderLayout.NORTH);
        panel.add(southButton, BorderLayout.SOUTH);
        panel.add(eastButton, BorderLayout.EAST);
        panel.add(westButton, BorderLayout.WEST);
        panel.add(centerButton, BorderLayout.CENTER);

        // Add the panel to the JFrame

        // Show the JFrame
        frame.addWindowListener(new WindowAdapter() {
        	public void windowClosing(WindowEvent e) {
           		frame.dispose(); // Close the frame

In this example, we first create a JFrame and set its size and default close operation. We then create a JPanel and set its layout to BorderLayout. Next, we create five JButton objects, one for each region of the BorderLayout. We then add each button to the JPanel, specifying the region using the appropriate BorderLayout constant Finally, we add the JPanel to the JFrame and make the JFrame visible.


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment