What is JPanel in Java with Examples

Hello, folks! This tutorial will explain how to use JPanel in Java with examples.

JPanel in Java with Examples

In this tutorial, we will understand the Java JPanel in a step-by-step manner. The topics that are covered in this tutorial are :

  • Introduction to JPanel
  • Constructors in JPanel
  • Methods in JPanel
  • Creation of a Basic JPanel

Furthermore, this tutorial will develop and build your Java skills and help you to make an application using JPanel. Do read this tutorial completely and at the end, I will provide some additional links. These links will consist of tutorials to further explore these topics.

Introduction to JPanel

The JPanel is a class of java.swing package which is extended from the Java AWT package. It is known for being the simplest form of container in creating lightweight window applications. It includes and can combine various components such as labels, buttons, and other panels also.

Above all, It is represented as a window not containing a title bar or border. As a result, it is basically the simplest container with the default layout as Flow Layout.

Constructors in JPanel

There are several ways to initialize a JPanel in Java. Let’s explore them.

  1. JPanel() – Creates a new JPanel with a buffer and a flow layout.
  2. JPanel(boolean isDoubleBuffered) – Initializes a new JPanel with identified buffering strategy.
  3. JPanel(LayoutManager layout) – Specifies a new JPanel with the given layout manager.

Let us see an implementation of one of the above constructors.

//Creation of a JPanel
JPanel panel = new JPanel();

Methods in JPanel

We will look at some of the most commonly used methods in JPanel class. I will define each of them with their definition below.

  1. add(Component c): Adds the specified component to the panel.
  2. setLayout(LayoutManager l): Sets the given layout manager of the panel.
  3. remove(Component c): Removes the specified component.
  4. paramString(): It is intended for debugging because it returns a string depiction of the JPanel.
  5. updateUI(): Resets the UI property.
  6. setUI(PanelUI ui): Sets the UI of an object that gets rendered.

As a result, these methods are very useful while dealing with application building a huge GUI.

Creation of a Basic JPanel

Firstly, we will create a basic JFrame and then create our JPanel. Secondly, we will now create a label and some buttons and then add them to the panel. Lastly, we will set the background color of the panel, add it to the frame and display it.

Let’s look at the code:

// Java Program to explain JPanel with an example

// Required Imports
import java.awt.*;
import javax.swing.*;

class JPanelExample extends JFrame {
  public static void main(String[] args)
  {
        //Creation of a JFrame 
        JFrame frame = new JFrame("JPanel Example");  
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setBounds(100, 200, 300, 300); 

        JLabel label = new JLabel("JLabel Title"); //Creating a JLabel with title
        JPanel panel = new JPanel(); // Creating a JPanel
        JButton button1,button2; // Initializing JButtons
        button1 = new JButton("Hello!"); //Creating a Button1 with a title 
        button2 = new JButton("Welcome to CodeSpeedy!");  //Creating a Button2 with a title
  
        panel.add(button1); //Adding the button1 to panel
        panel.add(button2); //Adding the button2 to panel
        panel.add(label); //Adding the leabel to panel
        panel.setBackground(Color.CYAN); //Setting the BG of the panel

        frame.add(panel); // Adding panel to frame
        frame.setVisible(true); //Displaying the frame
  }
}

Output:

JPanelExample

I hope you enjoyed reading this tutorial and upskilled your Java skills. Thanks for reading and keep learning!

Also Read:

Leave a Reply

Your email address will not be published.