VOOZH about

URL: https://www.geeksforgeeks.org/java/java-swing-jpanel-with-examples/

⇱ Java Swing - JPanel With Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Swing - JPanel With Examples

Last Updated : 11 Jul, 2025

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 

  1. JPanel(): creates a new panel with a flow layout
  2. JPanel(LayoutManager l): creates a new JPanel with specified layoutManager
  3. JPanel(boolean isDoubleBuffered): creates a new JPanel with a specified buffering strategy
  4. JPanel(LayoutManager l, boolean isDoubleBuffered): creates a new JPanel with specified layoutManager and a specified buffering strategy


Commonly used Functions of JPanel 

  1. add(Component c): Adds a component to a specified container
  2. setLayout(LayoutManager l): sets the layout of the container to the specified layout manager
  3. updateUI(): resets the UI property with a value from the current look and feel.
  4. setUI(PanelUI ui): sets the look and feel of an object that renders this component.
  5. getUI(): returns the look and feel object that renders this component.
  6. paramString(): returns a string representation of this JPanel.
  7. getUIClassID(): returns the name of the Look and feel class that renders this component.
  8. 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:

Output: 

👁 Image

Example 2:

Output:

👁 Image

 Example 3:

Output:

👁 Image

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
Article Tags: