CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > JPanel in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

JPanel in Java Swing

JPanel in java swing

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

JPanel is a container component in Java Swing that is used to hold and organize other components. It is often used as a building block for creating more complex user interfaces.

JPanel extends the JComponent class and provides methods for managing layout, adding and removing components, painting the panel, and handling events. Some common uses of JPanel include:

  1. Creating a custom panel with a specific layout and adding components to it.
  2. Creating a container for other components that can be added to a frame or another container.
  3. Providing a background color or image for a group of components.

To create a JPanel, we can simply call its default constructor:

JPanel panel = new JPanel();

We can then add components to the panel using the add() method:

panel.add(new JButton("Click me!"));

We can also set the layout manager for the panel using the setLayout() method. For example, to create a panel with a grid layout:

JPanel panel = new JPanel(new GridLayout(2, 2));
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
panel.add(new JButton("Button 3"));
panel.add(new JButton("Button 4"));

You can customize the appearance of the panel by setting its background color or image using the setBackground() method:

panel.setBackground(Color.WHITE);

Let’s see the complete example code mentioned above:

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JPanelDemo extends JFrame {
    
    public JPanelDemo() {
        setTitle("JPanel Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 200);
        setLocationRelativeTo(null);
        
        // Create a new panel with a grid layout
        JPanel panel = new JPanel(new GridLayout(2, 2));
        
        // Add buttons to the panel
        panel.add(new JButton("Button 1"));
        panel.add(new JButton("Button 2"));
        panel.add(new JButton("Button 3"));
        panel.add(new JButton("Button 4"));
        
        // Set the background color of the panel
        panel.setBackground(Color.WHITE);
        
        // Add the panel to the frame
        add(panel);
        
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new JPanelDemo();
    }
}

When we run this code, it will create a window with a panel containing four buttons arranged in a 2×2 grid. The panel’s background color will be set to white.

Output:

Jpanel demo

Though we set the JPanel background to white, it still does not show in the output because the JButton we’ve added is inside the JPanel.

Related Posts:

  • Using Color in Java Swing
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Event Handling in Java Swing
  • Java Swing Tutorial
  • List in Java
  • Adapter Classe in Java Swing
Tags:javaswing
Was this article helpful?
← Previous ArticleCreate Thread in Java
Next Article →Structured Query Language (SQL)

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap