Java Swing - JPanel With Examples Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar. Constructors of JPanel JPanel(): creates a new panel with a flow layoutJPanel(LayoutManager l): creates a new JPanel with specified layoutManagerJPanel(boolean isDoubleBuffered): creates a new JPanel with a specified buffering strategyJPanel(LayoutManager l, boolean isDoubleBuffered): creates a new JPanel with specified layoutManager and a specified buffering strategyCommonly used Functions of JPanel add(Component c): Adds a component to a specified containersetLayout(LayoutManager l): sets the layout of the container to the specified layout managerupdateUI(): resets the UI property with a value from the current look and feel.setUI(PanelUI ui): sets the look and feel of an object that renders this component.getUI(): returns the look and feel object that renders this component.paramString(): returns a string representation of this JPanel.getUIClassID(): returns the name of the Look and feel class that renders this component.getAccessibleContext(): gets the AccessibleContext associated with this JPanel. Let us take a sample program in order to illustrate the use of JPanel class by appending sequential execution snapshots of outputs justifying the below program sets as follows: Example: Java // Java Program to Create a Simple JPanel // and Adding Components to it // Importing required classes import java.awt.*; import java.awt.event.*; import javax.swing.*; // Class 1 // Helper class extending JFrame class class solution extends JFrame { // JFrame static JFrame f; // JButton static JButton b, b1, b2; // Label to display text static JLabel l; // Main class public static void main(String[] args) { // Creating a new frame to store text field and // button f = new JFrame("panel"); // Creating a label to display text l = new JLabel("panel label"); // Creating a new buttons b = new JButton("button1"); b1 = new JButton("button2"); b2 = new JButton("button3"); // Creating a panel to add buttons JPanel p = new JPanel(); // Adding buttons and textfield to panel // using add() method p.add(b); p.add(b1); p.add(b2); p.add(l); // setbackground of panel p.setBackground(Color.red); // Adding panel to frame f.add(p); // Setting the size of frame f.setSize(300, 300); f.show(); } } Output: Example 2: Java // Java Program to Create a JPanel with a Border Layout // and Adding Components to It // Importing required classes import java.awt.*; import java.awt.event.*; import javax.swing.*; // Main class // Extending JFrame class class solution extends JFrame { // JFrame static JFrame f; // JButton static JButton b, b1, b2, b3; // Label to display text static JLabel l; // Main driver method public static void main(String[] args) { // Creating a new frame to store text field and // button f = new JFrame("panel"); // Creating a label to display text l = new JLabel("panel label"); // Creating a new buttons b = new JButton("button1"); b1 = new JButton("button2"); b2 = new JButton("button3"); b3 = new JButton("button4"); // Creating a panel to add buttons // and a specific layout JPanel p = new JPanel(new BorderLayout()); // Adding buttons and textfield to panel // using add() method p.add(b, BorderLayout.NORTH); p.add(b1, BorderLayout.SOUTH); p.add(b2, BorderLayout.EAST); p.add(b3, BorderLayout.WEST); p.add(l, BorderLayout.CENTER); // setbackground of panel p.setBackground(Color.red); // Adding panel to frame f.add(p); // Setting the size of frame f.setSize(300, 300); f.show(); } } Output: Example 3: Java // Java Program to Create a JPanel with a Box layout // and Adding components to it // Importing required classes import java.awt.*; import java.awt.event.*; import javax.swing.*; // Main class // Extending JFrame class class solution extends JFrame { // JFrame static JFrame f; // JButton static JButton b, b1, b2, b3; // Label to display text static JLabel l; // Main drive method public static void main(String[] args) { // Creating a new frame to store text field and // button f = new JFrame("panel"); // Creating a label to display text l = new JLabel("panel label"); // Creating a new buttons b = new JButton("button1"); b1 = new JButton("button2"); b2 = new JButton("button3"); b3 = new JButton("button4"); // Creating a panel to add buttons and // textfield and a layout JPanel p = new JPanel(); // Setting box layout p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); // Adding buttons and textfield to panel p.add(b); p.add(b1); p.add(b2); p.add(b3); p.add(l); // Setting background of panel p.setBackground(Color.red); // Adding panel to frame f.add(p); // Setting the size of frame f.setSize(300, 300); f.show(); } } Output: Henceforth, we are successfully able to generate buttons in our panel. Note: In the previous Program, border layout and Box Layout are used. Different other layouts can be used to organize the components in a definite pattern, such as card layout, grid layout, etc. Comment More infoAdvertise with us Next Article Arrays in Java A andrew1234 Follow Improve Article Tags : Java Programming Language java-swing Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Arrays in Java In Java, an array is an important linear data structure that allows us to store multiple values of the same type. Arrays in Java are objects, like all other objects in Java, arrays implicitly inherit from the java.lang.Object class. This allows you to invoke methods defined in Object (such as toStri 9 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 9 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android 4 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Like