Java Documentation

JPanel: Grouping Components Inside a Frame

JPanel is a lightweight container used to group and organize other GUI components inside a JFrame. You will almost always use panels when building real applications.

Example: Using multiple JPanels in a JFrame

In this example, we create a frame and then use different panels for different regions of the window. Each panel can have its own background color and its own components.

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

class MyFrame extends JFrame {
    MyFrame() {
        setTitle("Components - JPanel");

        /* General Example:

        JPanel p1 = new JPanel();
        add(p1);

        */

        // we can use different panels for different purposes:

        JPanel topPanel = new JPanel();
        JPanel bottomPanel = new JPanel();

        topPanel.setBackground(Color.LIGHT_GRAY);
        bottomPanel.setBackground(Color.CYAN);

        topPanel.add(new JLabel("Top Panel"));
        bottomPanel.add(new JButton("Click Me"));

        add(topPanel, BorderLayout.NORTH);
        add(bottomPanel, BorderLayout.CENTER);

        // setLayout(new FlowLayout());
        setSize(500, 500);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

public class JPanelDemo {
    public static void main(String[] args) {
        new MyFrame();
    }
}
Reference & full codeView on GitHub

What is JPanel?

JPanel is a generic lightweight container. Instead of adding all buttons, labels, and text fields directly to the frame, we place them inside panels. This helps us group related components and control layout more easily.

  • You can create multiple panels (e.g., top, center, bottom) for different sections of the window.
  • Each panel can have its own layout manager and background color.
  • Panels are added to the frame like any other component, often using a container layout such as BorderLayout.

Line-by-line: Understanding the JPanel example

Let's focus on how the panels are created, customized, and added to the frame. The JFrame setup is the same as in the previous section.

JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();

Creates two panel objects. Each panel is a small container that can hold its own set of components.

topPanel.setBackground(Color.LIGHT_GRAY);
bottomPanel.setBackground(Color.CYAN);

Sets different background colors so you can clearly see the separation between the panels on the screen.

topPanel.add(new JLabel("Top Panel"));
bottomPanel.add(new JButton("Click Me"));

Adds a label to the top panel and a button to the bottom panel. Each panel can contain multiple components, arranged according to its own layout manager.

add(topPanel, BorderLayout.NORTH);
add(bottomPanel, BorderLayout.CENTER);

Adds the panels to the frame using BorderLayout regions:NORTH for the top area and CENTER for the remaining middle area. This is a very common pattern in Swing GUIs.

Why use multiple panels?

Using several panels instead of putting everything directly into the frame makes your code easier to manage and extend. You can:

  • Group related controls together (e.g., a form panel, a button bar).
  • Change the layout of one part of the window without affecting others.
  • Reuse panels in different frames or dialogs.