Using Layout Managers in Swing for Component Placement

In Swing, every component occupies a specific position and size with in its container. Managing these coordinates manually to multiple components is impractical. Layout managers provide a systematic way to arrange and display components within containers, handling positioning and resizing automatically.

Three commonly used layout managers in Swing are FlowLayout, BorderLayout, and GridLayout.

FlowLayout

FlowLayout arranges components in a directional flow, much like words in a paragraph. Components are placed left-to-right by default, and when a row is filled, a new row begins. The constructor allows specifying alignment, horizontal gap, and vertical gap.

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

public class FlowLayoutExample extends JFrame {
    public FlowLayoutExample() {
        setTitle("FlowLayout Demo");
        Container contentPane = getContentPane();
        // Alignment: FlowLayout.RIGHT (2), horizontal gap 10, vertical gap 10
        setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        for (int i = 0; i < 10; i++) {
            contentPane.add(new JButton("Btn" + i));
        }
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new FlowLayoutExample();
    }
}

BorderLayout

BorderLayout divides the container into five regions: NORTH, SOUTH, WEST, EAST, and CENTER. Each region can contain at most one component. When adding a component, you specify the region constant.

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

public class BorderLayoutExample extends JFrame {
    public BorderLayoutExample() {
        setTitle("BorderLayout Demo");
        Container contentPane = getContentPane();
        setLayout(new BorderLayout());
        contentPane.add(BorderLayout.CENTER, new JButton("Center"));
        contentPane.add(BorderLayout.NORTH, new JButton("North"));
        contentPane.add(BorderLayout.SOUTH, new JButton("South"));
        contentPane.add(BorderLayout.WEST, new JButton("West"));
        contentPane.add(BorderLayout.EAST, new JButton("East"));
        setSize(350, 200);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new BorderLayoutExample();
    }
}

GridLayout

GridLayout arranges componants in a rectangular grid of equally sized cells. The constructor takes the number of rows, columns, horizontal gap, and vertical gap. Components are added row by row.

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

public class GridLayoutExample extends JFrame {
    public GridLayoutExample() {
        setTitle("GridLayout Demo");
        Container contentPane = getContentPane();
        // 3 rows, 4 columns, hgap 5, vgap 5
        setLayout(new GridLayout(3, 4, 5, 5));
        for (int i = 0; i < 12; i++) {
            contentPane.add(new JButton("B" + i));
        }
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new GridLayoutExample();
    }
}

These managers form the foundation for component layout in Swing applications. Choose the one that best fits your design needs.

Tags: swing Layout Managers FlowLayout BorderLayout GridLayout

Posted on Fri, 14 Aug 2026 16:14:32 +0000 by discofreakboot